JavaScript Fundamentals

Description: JavaScript Fundamentals Quiz
Number of Questions: 15
Created by:
Tags: javascript fundamentals programming
Attempted 0/15 Correct 0 Score 0

What is the correct syntax for declaring a variable in JavaScript?

  1. var variableName;

  2. let variableName;

  3. const variableName;

  4. All of the above


Correct Option: D
Explanation:

In JavaScript, you can declare a variable using the var, let, or const keyword, followed by the variable name and a semicolon.

What is the difference between var, let, and const in JavaScript?

  1. var is used for global variables, let is used for local variables, and const is used for constants.

  2. var is used for variables that can be reassigned, let is used for variables that cannot be reassigned, and const is used for constants.

  3. var is used for variables that are declared at the top of a function, let is used for variables that are declared within a block, and const is used for constants.

  4. None of the above


Correct Option:
Explanation:

In JavaScript, let is used for variables that cannot be reassigned, and const is used for constants. var is used for variables that can be reassigned, but it is generally discouraged to use var in modern JavaScript code.

What is the correct syntax for creating a function in JavaScript?

  1. function functionName() { ... }

  2. const functionName = () => { ... }

  3. let functionName = () => { ... }

  4. All of the above


Correct Option: D
Explanation:

In JavaScript, you can create a function using the function keyword, followed by the function name and parentheses. You can also create a function using an arrow function, which is a concise syntax for writing a function. Arrow functions can be written using the const or let keyword, followed by the arrow (=>) symbol and the function body.

What is the difference between a function declaration and a function expression in JavaScript?

  1. A function declaration is hoisted to the top of the scope, while a function expression is not.

  2. A function declaration can be called before it is defined, while a function expression cannot.

  3. A function declaration can be used as a value, while a function expression cannot.

  4. None of the above


Correct Option: A
Explanation:

In JavaScript, a function declaration is hoisted to the top of the scope, which means that it can be called before it is defined. A function expression is not hoisted, so it cannot be called before it is defined.

What is the correct syntax for creating an array in JavaScript?

  1. const arrayName = [];

  2. let arrayName = [];

  3. var arrayName = [];

  4. All of the above


Correct Option: D
Explanation:

In JavaScript, you can create an array using the const, let, or var keyword, followed by the array name and square brackets.

What is the difference between an array and an object in JavaScript?

  1. An array is an ordered collection of values, while an object is an unordered collection of key-value pairs.

  2. An array can only store primitive values, while an object can store any type of value.

  3. An array can be accessed using an index, while an object can be accessed using a key.

  4. All of the above


Correct Option: D
Explanation:

In JavaScript, an array is an ordered collection of values, while an object is an unordered collection of key-value pairs. An array can only store primitive values, while an object can store any type of value. An array can be accessed using an index, while an object can be accessed using a key.

What is the correct syntax for creating an object in JavaScript?

  1. const objectName = {};

  2. let objectName = {};

  3. var objectName = {};

  4. All of the above


Correct Option: D
Explanation:

In JavaScript, you can create an object using the const, let, or var keyword, followed by the object name and curly braces.

What is the difference between a property and a method in JavaScript?

  1. A property is a key-value pair, while a method is a function.

  2. A property can be accessed using a dot (.) operator, while a method can be called using parentheses.

  3. A property can be changed, while a method cannot.

  4. None of the above


Correct Option: A
Explanation:

In JavaScript, a property is a key-value pair, while a method is a function. A property can be accessed using a dot (.) operator, while a method can be called using parentheses. A property can be changed, while a method cannot.

What is the correct syntax for accessing a property of an object in JavaScript?

  1. objectName.propertyName

  2. objectName['propertyName']

  3. Both of the above

  4. None of the above


Correct Option: C
Explanation:

In JavaScript, you can access a property of an object using the dot (.) operator or the square bracket ([]) notation. The dot operator is used when the property name is a valid JavaScript identifier, while the square bracket notation is used when the property name is not a valid JavaScript identifier.

What is the correct syntax for calling a method of an object in JavaScript?

  1. objectName.methodName()

  2. objectName'methodName'

  3. Both of the above

  4. None of the above


Correct Option: C
Explanation:

In JavaScript, you can call a method of an object using the dot (.) operator or the square bracket ([]) notation. The dot operator is used when the method name is a valid JavaScript identifier, while the square bracket notation is used when the method name is not a valid JavaScript identifier.

What is the correct syntax for creating a loop in JavaScript?

  1. for (let i = 0; i < array.length; i++) { ... }

  2. for (const i in array) { ... }

  3. for (const i of array) { ... }

  4. All of the above


Correct Option: D
Explanation:

In JavaScript, there are three types of loops: the for loop, the for...in loop, and the for...of loop. The for loop is used to iterate over a range of numbers, the for...in loop is used to iterate over the properties of an object, and the for...of loop is used to iterate over the elements of an array.

What is the difference between a break statement and a continue statement in JavaScript?

  1. A break statement exits the loop immediately, while a continue statement skips the current iteration of the loop and continues with the next iteration.

  2. A break statement exits the loop immediately, while a continue statement exits the current iteration of the loop and starts the next iteration.

  3. A break statement skips the current iteration of the loop and continues with the next iteration, while a continue statement exits the loop immediately.

  4. None of the above


Correct Option: A
Explanation:

In JavaScript, a break statement exits the loop immediately, while a continue statement skips the current iteration of the loop and continues with the next iteration.

What is the correct syntax for creating a conditional statement in JavaScript?

  1. if (condition) { ... }

  2. if (condition) { ... } else { ... }

  3. if (condition) { ... } else if (condition) { ... } else { ... }

  4. All of the above


Correct Option: D
Explanation:

In JavaScript, there are three types of conditional statements: the if statement, the if...else statement, and the if...else if...else statement. The if statement is used to execute a block of code if a condition is true, the if...else statement is used to execute one block of code if a condition is true and another block of code if the condition is false, and the if...else if...else statement is used to execute one block of code if a condition is true, another block of code if a different condition is true, and a third block of code if both conditions are false.

What is the correct syntax for creating a switch statement in JavaScript?

  1. switch (expression) { case value1: ...; break; case value2: ...; break; ...; default: ...; }

  2. switch (expression) { case value1: ...; case value2: ...; ...; default: ...; }

  3. switch (expression) { case value1: ...; break; case value2: ...; ...; }

  4. None of the above


Correct Option: A
Explanation:

In JavaScript, the switch statement is used to execute different blocks of code depending on the value of an expression. The switch statement is followed by an expression, which is evaluated. The value of the expression is then compared to the values of the case statements. If the value of the expression matches the value of a case statement, the code within that case statement is executed. The break statement is used to exit the switch statement after a case statement has been executed.

What is the correct syntax for creating a function in JavaScript?

  1. function functionName() { ... }

  2. const functionName = () => { ... }

  3. let functionName = () => { ... }

  4. All of the above


Correct Option: D
Explanation:

In JavaScript, you can create a function using the function keyword, followed by the function name and parentheses. You can also create a function using an arrow function, which is a concise syntax for writing a function. Arrow functions can be written using the const or let keyword, followed by the arrow (=&gt;) symbol and the function body.

- Hide questions