JavaScript Basics
Ready to sharpen your JavaScript skills? Dive into our interactive quiz practice questions, designed to test your knowledge from the fundamentals to more advanced concepts. Whether you're a beginner looking to solidify your understanding or an experienced developer aiming to refresh your memory, these quizzes offer a fantastic way to learn and improve. Each question is crafted to challenge you and reinforce key JavaScript principles. Get ready to code, learn, and master JavaScript, one question at a time! Good luck!
1. Which of the following is not belongs to primitive data type in JavaScript?
Answer:
Explanation:
In JavaScript, the primitive data types are Number, String, Boolean, Null, and Undefined. Object is not a primitive data type.
2. What is the result of the following code: var x = 10; console.log(x++);
Answer:
Explanation:
The "++" operator increments the value of the variable by 1 after the value is returned. Therefore, the output of the code is 10.
3.What is the output of the following code: var x = true; console.log(!x);
Answer:
Explanation:
The "!" operator inverts the value of a boolean expression. Since the value of "x" is true, "!x" will evaluate to false.
4. What is the meaning of "NaN" in JavaScript?
Answer:
Explanation:
The "NaN" value in JavaScript represents "Not a Number" and is returned when a mathematical operation fails to produce a valid number.
5. What is the result of the following code:
var x = 5;
var y = "5";
console.log(x == y);
Answer:
Explanation:
The "==" operator in JavaScript performs type coercion, which means that the values are converted to a common type before being compared. In this case, the string "5" is converted to the number 5, so the values of x and y are equal.
6. What is the correct way to declare a variable in JavaScript?
Answer:
Explanation:
n modern JavaScript, the recommended way to declare a variable is using the "let" keyword.
7. What does the "this" keyword refer to in JavaScript?
Answer:
Explanation:
The "this" keyword in JavaScript refers to the object that the function belongs to. The value of "this" is determined at runtime based on how the function is called.
8. What is the result of the following code:
var x = [1, 2, 3];
console.log(x.length);
Answer:
Explanation:
The "length" property in JavaScript returns the number of elements in an array. In this case, the array "x" has three elements, so the result is 3.
9. What is the result of the following code: console.log(typeof NaN);
Answer:
Explanation:
Although NaN stands for "Not a Number", its type in JavaScript is actually "number".
10. What does the "forEach" method do in JavaScript?
Answer:
Explanation:
The "forEach" method in JavaScript executes a provided function once for each element in an array.
11. What is the result of the following code: console.log(2 ** 3);
Answer:
Explanation:
The "**" operator in JavaScript is the exponentiation operator, which raises the first operand to the power of the second operand. In this case, 2 raised to the power of 3 is 8.
12. What is the correct syntax for a "for" loop in JavaScript?
Answer:
Explanation:
The correct syntax for a "for" loop in JavaScript includes the initialization statement, the condition, and the increment statement, all separated by semicolons.
13. What is the result of the following code:
var x = 5;
var y = "5";
console.log(x === y);
Answer:
Explanation:
The "===" operator in JavaScript performs a strict comparison, which means that the values and the types must be equal for the comparison to return true. In this case, x is a number and y is a string
14. What is the difference between "==" and "===" operators in JavaScript?
Answer:
Explanation:
The "===" operator in JavaScript performs a strict comparison, which means that the values and the types must be equal for the comparison to return true. The "==" operator performs a loose comparison, which means that it tries to convert the operands to the same type before making the comparison.
15. What is the result of the following code:
var x = 10; var y = "5"; console.log(x - y);
Answer:
Explanation:
When JavaScript tries to perform arithmetic operations with a string and a number, it automatically converts the string to a number before making the calculation. In this case, "5" is converted to 5, so the result of the subtraction is 5.
16. What is the result of the following code:
var x = [1, 2, 3]; var y = [...x]; console.log(y);
Answer:
Explanation:
The spread operator (...) in JavaScript allows you to copy the elements of an array into a new array. In this case, the variable "y" is assigned a new array that contains the same elements as "x".
17. What is the result of the following code: console.log(Math.random());
Answer:
Explanation:
The Math.random() function in JavaScript generates a random number between 0 (inclusive) and 1 (exclusive) each time it is called.
18. What is the difference between "let" and "const" keywords in JavaScript?
Answer:
Explanation:
In JavaScript, "let" and "const" are used to declare variables. The difference is that "let" variables can be reassigned a new value, while "const" variables cannot be reassigned a new value after they are declared. Both "let" and "const" variables are block-scoped.
19. Which of the following is not a data type in JavaScript?
Answer:
Explanation:
In JavaScript, there is no data type called "Character". Instead, characters are represented using the "String" data type.
20. What is the result of the following code: console.log("5" + 5);
Answer:
Explanation:
When you use the "+" operator with a string and a number, JavaScript converts the number to a string and concatenates it to the string.
21. What is the difference between "var" and "let" keywords in JavaScript?
Answer:
Explanation:
In JavaScript, "var" and "let" are used to declare variables. The difference is that "let" variables have block scope, which means they are only accessible within the block they are declared in, while "var" variables have function scope, which means they are accessible throughout the function they are declared in.
22. What is the result of the following code:
var x = 10;
if (true) {
var x = 5;
} console.log(x);
Answer:
Explanation:
In JavaScript, "var" variables are function-scoped, not block-scoped. This means that the "x" variable inside the if statement is the same as the "x" variable outside of it, and changing its value inside the block also changes its value outside the block.
23. What is the result of the following code:
var x = [1, 2, 3]; console.log(x[3]);
Answer:
Explanation:
In JavaScript, arrays are zero-indexed, which means that the first element of an array has an index of 0. Since the "x" array has only three elements, attempting to access the fourth element using an index of 3 results in undefined.
24. Which of the following is not a valid way to declare a function in JavaScript?
Answer:
Explanation:
The syntax for declaring a function in JavaScript is either "function functionName() {}" or "var functionName = function() {}", or using an arrow function expression "() => {}". The option
25. What is the difference between "==" and "===" operators in JavaScript?
Answer:
Explanation:
The "==" operator checks for value equality, and it performs type coercion if the types of the two values being compared are different. The "===" operator checks for value and type equality, and it does not perform type coercion.
26. What is the result of the following code: console.log(typeof undefine
Answer:
Explanation:
"undefined" is a primitive data type in JavaScript that represents the absence of a value. The typeof operator returns a string indicating the type of the operand.
27. What is the result of the following code:console.log(1 + "2" + 3);
Answer:
Explanation:
When you use the "+" operator with a string and a number, JavaScript converts the number to a string and concatenates it to the string. So in this case, "1" is concatenated with "2" to create "12", and then "3" is concatenated to create "123".
28. Which of the following is not a loop in JavaScript?
Answer:
Explanation:
"next" is not a loop in JavaScript. The correct loop keywords are "for", "while", and "do...while".
29. Which of the following is not a valid JavaScript array method?
Answer:
Explanation:
"slice()" is a valid array method in JavaScript, but it is not a method for modifying the array. Instead, it returns a shallow copy of a portion of the array as a new array.
30. What is the result of the following code: console.log(typeof []);
Answer:
Explanation:
In JavaScript, arrays are a type of object. The typeof operator returns "object" for any object type, including arrays.
31. What is the result of the following code:
var x = 10;
function foo() {
console.log(x);
var x = 5;
} foo();
Answer:
Explanation:
In JavaScript, variable declarations are hoisted to the top of the function, but their assignments are not. So the "x" variable inside the "foo" function is hoisted, but it is not assigned a value until after the console.log statement. Therefore, the result is "undefined".
32. What is the result of the following code:
var x = 1;
function foo() {
var x = 2;
function bar() {
console.log(x);
} return bar;
} var baz =
foo();
baz();
Answer:
Explanation:
In JavaScript, inner functions have access to the variables of their outer functions, even after the outer function has returned. In this case, the "baz" variable is assigned the "bar" function returned by "foo", and when "baz" is called, it accesses the "x" variable of its parent function "foo".
33. What is the result of the following code: console.log("5" == 5);
Answer:
Explanation:
In JavaScript, the "==" operator performs type coercion before comparison. In this case, the string "5" is converted to the number 5 before comparison with the number 5, resulting in true.
34. Which of the following is not a valid way to declare a variable in JavaScript?
Answer:
Explanation:
"variable" is not a valid keyword for declaring variables in JavaScript. The correct keyword is "var", "let", or "const".
35. What is the result of the following code: console.log(typeof null);
Answer:
Explanation:
In JavaScript, the typeof operator returns "object" for the null value. This is considered to be a historical bug in the language.
36. What is the result of the following code: console.log("Hello".charAt(1));
Answer:
Explanation:
In JavaScript, the "charAt" method is used to access a character in a string by its index. In this case, the character at index 1 is "e".
37. What is the result of the following code:
var x = 5;
function foo() {
console.log(x);
} function bar() {
var x = 10;
foo();
} bar();
Answer:
Explanation:
In JavaScript, inner functions have access to the variables of their outer functions, even if the outer function has already returned. In this case, the "foo" function accesses the "x" variable in the global scope, which has a value of 5. The "x" variable declared in the "bar" function is a separate variable with a different scope.
38. What is the result of the following code: console.log(2 + 3 + "4");
Answer:
Explanation:
In JavaScript, the "+" operator performs both addition and string concatenation. When the "+" operator is used with both numbers and strings, it performs addition first and then concatenates the result with the remaining string. In this case, 2 + 3 = 5, and then "4" is concatenated to create "54".
39. Which of the following is not a valid way to create a JavaScript array?
Explanation:
Curly braces are used to create JavaScript objects, not arrays. The correct way to create an array is to use square brackets or the Array constructor.
40. What is the result of the following code: console.log(NaN == NaN);
Answer:
Explanation:
In JavaScript, NaN is a special value that represents "Not a Number". NaN is not equal to any other value, including itself.
41. What is the result of the following code:
var x = 5;
var y = x++;
console.log(y);
Answer:
Explanation:
In JavaScript, the "++" operator is used to increment a variable by 1. When used after the variable (i.e. x++), the increment happens after the current value is used. In this case, the value of x is 5, so y is assigned the value of 5 before x is incremented to 6.
42. What is the result of the following code:
var x = [1, 2, 3];
var y = x.slice(1);
console.log(y);
Answer:
Explanation:
In JavaScript, the "slice" method is used to create a new array that contains a portion of an existing array. In this case, the "y" variable is assigned a new array that contains all elements of "x" starting from index 1 (i.e. [2, 3]).
43. What is the result of the following code:
var x = [1, 2, 3];
console.log(x.length);
Explanation:
In JavaScript, the "length" property of an array returns the number of elements in the array. In this case, the "x" array has 3 elements, so the result is 3.
44. What is the result of the following code: var x = "hello"; console.log(x.charAt(0));
Answer:
Explanation:
In JavaScript, the "charAt" method is used to get the character at a specified index in a string. In this case, "x" is the string "hello", and "charAt(0)" returns the first character, which is "h".
45. What is the result of the following code:
var x = 5;
console.log(typeof x);
Answer:
Explanation:
In JavaScript, the "typeof" operator is used to get the type of a value or variable. In this case, "x" is assigned the value of 5, which is a number, so the result is "number".
46. What is the result of the following code:
var x = "5";
var y = 2;
console.log(x + y);
Answer:
Explanation:
In JavaScript, the "+" operator performs both addition and string concatenation. When the "+" operator is used with a string and a number, the number is converted to a string and then concatenated with the original string. In this case, "x" is the string "5", and "y" is the number 2, so "x + y" concatenates the two values to create the string "52".
47. What is the result of the following code:
var x = [1, 2, 3]; x.push(4); console.log(x);
Answer:
Explanation:
In JavaScript, the "push" method is used to add an element to the end of an array. In this case, the "x" array already contains the elements [1, 2, 3], and the "push(4)" statement adds the element 4 to the end of the array, resulting in the new array [1, 2, 3, 4].
48. What is the result of the following code:
var x = 10; var y = "5"; console.log(x - y);
Answer:
Explanation:
In JavaScript, when the "-" operator is used with a number and a string, the string is converted to a number and the subtraction is performed. In this case, "x" is the number 10, and "y" is the string "5". However, because the "-" operator can also be used for string concatenation, the string "5" is first converted to the number 5, and then subtracted from 10 to get 15.
49. What is the result of the following code:
var x = "hello"; console.log(x.toUpperCase());
Answer:
Explanation:
In JavaScript, the "toUpperCase" method is used to convert a string to all uppercase letters. In this case, "x" is the string "hello", and "toUpperCase()" converts it to "HELLO".
50. What is the result of the following code:
var x = true; var y = false; console.log(x || y);
Answer:
Explanation:
In JavaScript, the "||" operator is the logical OR operator, and returns true if either operand is true. In this case, "x" is true, and "y" is false, so the expression "x || y" evaluates to true.
51. What is the result of the following code:
var x = [1, 2, 3];
console.log(x[0]);
Answer:
Explanation:
In JavaScript, arrays are indexed starting from 0. The square brackets notation is used to access an element of an array by its index. In this case, "x" is the array [1, 2, 3], and "x[0]" accesses the first element of the array, which is the number 1.
52. What is the result of the following code:
var x = 5;
var y = "10";
console.log(x + y);
Answer:
Explanation:
In JavaScript, the "+" operator performs both addition and string concatenation. When the "+" operator is used with a number and a string, the number is converted to a string and then concatenated with the original string. In this case, "x" is the number 5, and "y" is the string "10", so "x + y" concatenates the two values to create the string "510".
53. What is the result of the following code:
var x = [1, 2, 3]; x.push(4); console.log(x);
Answer:
Explanation:
In JavaScript, the "push" method is used to add an element to the end of an array. In this case, "x" is the array [1, 2, 3], and "x.push(4)" adds the number 4 to the end of the array to create the array [1, 2, 3, 4].
54. What is the result of the following code:
var x = "10"; var y = "5"; console.log(x + y);
Answer:
Explanation:
In JavaScript, the "+" operator performs both addition and string concatenation. When the "+" operator is used with two strings, the two strings are concatenated. In this case, "x" is the string "10", and "y" is the string "5", so "x + y" concatenates the two strings to create the string "105".
55. What is the result of the following code:
var x = 10;
console.log(typeof x);
Answer:
Explanation:
In JavaScript, the "typeof" operator is used to determine the data type of a value. In this case, "x" is the number 10, so "typeof x" returns the string "number".
56. What is the result of the following code:
var x = [1, 2, 3]; console.log(x.length);
Answer:
Explanation:
In JavaScript, the "length" property is used to get the number of elements in an array. In this case, "x" is the array [1, 2, 3], and "x.length" returns the number 3.
57. What is the result of the following code:
var x = "10"; var y = "5"; console.log(x - y);
Answer:
Explanation:
In JavaScript, when the "-" operator is used with two strings, both strings are converted to numbers and the subtraction is performed. In this case, "x" is the string "10", and "y" is the string "5". Both strings are converted to numbers (10 and 5, respectively), and then 5 is subtracted from 10 to get 5.
58. What is the result of the following code:
var x = 5; console.log(++x);
Explanation:
In JavaScript, the "++" operator is used to increment a variable by 1. When the "++" operator is used before the variable (like in this example), the variable is first incremented and then used. So, "++x" increments the variable "x" from 5 to 6, and then returns the value 6.
59. What is the result of the following code:
var x = [1, 2, 3]; console.log(x[1]);
Answer:
Explanation:
In JavaScript, arrays are zero-indexed, meaning the first element in the array has an index of 0, the second element has an index of 1, and so on. In this case, "x" is the array [1, 2, 3], and "x[1]" accesses the second element of the array (which has an index of 1), which is the number 2.
60. What is the result of the following code:
var x = 10;
if (x == "10") {
console.log("Equal");
} else {
console.log("Not equal");
}
Answer:
Explanation:
In JavaScript, the "==" operator performs a loose equality comparison, which means it compares the values without considering their data types. In this case, "x" is the number 10, and "x == '10'" returns true because the string "10" is converted to the number 10 before the comparison is made. So, the "if" statement evaluates to true, and "Equal" is printed to the console.
61. What is the result of the following code:
var x = "hello"; console.log(x.toUpperCase());
Explanation:
In JavaScript, the "toUpperCase" method is used to convert a string to all uppercase letters. In this case, "x" is the string "hello", and "x.toUpperCase()" converts the string to "HELLO" and prints it to the console.
62. What is the result of the following code:
var x = true; var y = false; console.log(x || y);
Answer:
Explanation:
In JavaScript, the "||" operator performs a logical OR operation. The expression "x || y" evaluates to true if either "x" or "y" is true. In this case, "x" is true, so the expression "x || y" evaluates to true and is printed to the console.
63. What is the result of the following code:
var x = "5"; console.log(typeof x);
Explanation:
In JavaScript, the "typeof" operator is used to determine the data type of a variable. In this case, "x" is a string because it is enclosed in quotation marks.
64. What is the result of the following code:
var x = 3; var y = "4"; console.log(x + y);
Answer:
Explanation:
In JavaScript, the "+" operator is used to concatenate strings and add numbers. If one or both operands are strings, the "+" operator performs string concatenation. In this case, "x" is the number 3 and "y" is the string "4", so the expression "x + y" converts the number 3 to a string and concatenates it with the string "4", resulting in the string "34".
65. What is the result of the following code:
var x = [1, 2, 3]; x.push(4); console.log(x);
Answer:
Explanation:
In JavaScript, the "push" method is used to add elements to the end of an array. In this case, "x" is the array [1, 2, 3], and "x.push(4)" adds the number 4 to the end of the array, resulting in the array [1, 2, 3, 4].
66. What is the result of the following code:
var x = 5;
if (x === "5") {
console.log("Equal");
} else {
console.log("Not equal");
}
Answer:
Explanation:
In JavaScript, the "===" operator performs a strict equality comparison, which means it compares the values and data types of the operands. In this case, "x" is the number 5, and "x === '5'" returns false because the string "5" is not the same data type as the number 5. So, the "if" statement evaluates to false, and "Not equal" is printed to the console.
67. What is the result of the following code:
var x = 10;
while (x > 0) {
console.log(x);
x--;
}
Answer:
Explanation:
In JavaScript, the "while" loop is used to repeatedly execute a block of code while a condition is true. In this case, the loop will execute as long as "x" is greater than 0. In each iteration of the loop, the value of "x" is printed to the console using the "console.log" function, and then "x" is decremented by 1.
68. What is the result of the following code:
var x = [1, 2, 3]; var y = x; y.push(4); console.log(x);
Answer:
Explanation:
In JavaScript, arrays and objects are reference types, which means that when a variable is assigned to an array or object, it holds a reference to the location in memory where the array or object is stored. In this case, "y" is assigned to the same array as "x" using the assignment operator, so both variables reference the same array. When "y.push(4)" is called, it modifies the array that both "x" and "y" reference, so the result of "console.log(x)" is the modified array [1, 2, 3, 4].
69. What is the result of the following code:
var x = 10; var y = x++; console.log(y);
Answer:
Explanation:
In JavaScript, the "++" operator is used to increment a variable by 1. When "x++" is called, the value of "x" is first used as the operand of the expression, and then it is incremented by 1. The value of the expression "x++" is the original value of "x" before it was incremented, so "y" is assigned the value 10. The result of "console.log(y)" is therefore 10.
70. What is the result of the following code:
var x = 5; var y = 3; console.log(x *= y);
Explanation:
In JavaScript, the "*=" operator is used to perform multiplication and assignment in a single step. The expression "x *= y" is equivalent to "x = x * y", which multiplies the value of "x" by the value of "y" and assigns the result back to "x". The result of "console.log(x *= y)" is therefore the product of 5 and 3, which is 15.
71. What is the result of the following code:
var x = "10"; var y = +"3"; console.log(x + y);
Answer:
Explanation:
In JavaScript, the unary "+" operator can be used to convert a string to a number. When the expression "+ '3'" is evaluated, the string "3" is converted to the number 3. The expression "x + y" then concatenates the string "10" with the string "3", resulting in the string "13". The result of "console.log(x + y)" is therefore "13".
72. What is the result of the following code:
var x = 0;
while (x < 5) {
console.log(x);
x += 2;
}
Answer:
73. What is the result of the following code:
console.log("5" + 2);Answer:
Explanation:
In JavaScript, the "+" operator is used for both addition and string concatenation. When one or both operands are strings, the "+" operator concatenates the strings. In this case, the string "5" is concatenated with the number 2, resulting in the string "52". The result of "console.log("5" + 2)" is therefore "52".
74. What is the result of the following code:
var x = [1, 2, 3]; console.log(x.slice(1));
Answer:
Explanation:
In JavaScript, the "slice" method is used to create a new array that contains a portion of an existing array. The "slice" method takes two arguments: the starting index (inclusive) and the ending index (exclusive) of the portion to be sliced. If the ending index is omitted, the "slice" method returns a new array that includes all elements from the starting index to the end of the original array. In this case, "x.slice(1)" creates a new array that includes all elements from index 1 (i.e., the second element) to the end of the "x" array. The result of "console.log(x.slice(1))" is therefore the array [2, 3].
75. What is the result of the following code:
var x = 5;
var y = 3;
if (x > y) {
console.log("x is greater than y");
} else {
console.log("y is greater than x");
}
Answer:
Explanation:
In this code, an "if" statement is used to check whether the value of "x" is greater than the value of "y". If the condition is true, the first block of code (i.e., the "console.log" statement) is executed. Otherwise, the second block of code is executed. In this case, the condition is true because 5 is greater than 3, so the result of "console.log" is "x is greater than y".
76. What is the result of the following code:
var x = 10;
function foo() {
console.log(x);
var x = 5;
console.log(x);
} foo();
Answer:
Explanation:
In JavaScript, variable declarations are "hoisted" to the top of their scope, which means that they are processed before any other code in the same scope. However, variable assignments are not hoisted. In this case, the "foo" function includes a variable declaration "var x = 5" after the first "console.log" statement. This means that the "x" variable used in the first "console.log" statement refers to the local variable declared in the "foo" function, not the global variable with the same name. However, at the time of the first "console.log" statement, the local variable "x" has not yet been assigned a value, so its value is undefined.
77. What is the result of the following code: console.log(typeof null);
Answer:
Explanation:
In JavaScript, the "typeof" operator is used to determine the data type of a value or variable. When used with the "null" keyword, "typeof" returns "object". This is a historical artifact of JavaScript, as "null" was originally intended to represent an empty object reference.
78. What is the result of the following code:
var x = [1, 2, 3]; var y = x; y.push(4); console.log(x);
Explanation:
In JavaScript, arrays are objects, and objects are passed by reference rather than by value. This means that when "y" is assigned the value of "x", it is actually assigned a reference to the same array in memory. Therefore, when "y.push(4)" is called, it modifies the same array that "x" refers to. The result of "console.log(x)" is therefore [1, 2, 3, 4].
79. What is the result of the following code: console.log(5 == "5");
Answer:
Explanation:
In JavaScript, the "==" operator is used to compare two values for equality. When the values being compared have different data types, JavaScript will attempt to convert one or both of the values to a common data type before making the comparison. In this case, the string "5" is converted to the number 5 before the comparison is made. Therefore, the result of "console.log(5 == "5")" is true.
80. What is the result of the following code:
var x = 10;
function foo() {
console.log(x);
} foo();
var x = 5;
Explanation:
In JavaScript, function declarations are processed before any other code in the same scope. Therefore, the "foo" function is defined before the global variable "x" is assigned the value 5. When "foo" is called, it results the current value of the global variable "x", which is 10 at the time of the call. The result of "console.log(x)" is therefore 10.
81. What is the result of the following code:
var x = 5; var y = 3; var z = x++ - y--; console.log(z);
Answer:
Explanation:
In JavaScript, the "++" and "--" operators are used to increment and decrement a variable's value by 1, respectively. The placement of these operators determines whether the variable's value is incremented or decremented before or after an expression is evaluated. In this case, the expression "x++ - y--" is evaluated as follows: ? The value of "x" (5) is used in the expression, and then incremented to 6. ? The value of "y" (3) is used in the expression, and then decremented to 2.
82. What is the result of the following code: console.log("hello" + 3);
Explanation:
In JavaScript, the "+" operator is used for both addition and concatenation. When used with a string and a number, it performs concatenation rather than addition. Therefore, the result of "console.log("hello" + 3)" is "hello3".
83. What is the result of the following code: console.log(true && "hello");
Answer:
Explanation:
In JavaScript, the "&&" operator is used for logical AND. It returns the first falsy value it encounters, or the last truthy value if all values are truthy. In this case, both "true" and "hello" are truthy values, so the expression evaluates to "hello".
84. What is the result of the following code:
var x = 5;
function foo() {
console.log(x);
var x = 10;
} foo();
Explanation:
In JavaScript, variable declarations are processed before any other code in the same scope, but their values are not assigned until they are reached in the code. Therefore, the "var x = 10;" statement in the "foo" function does not affect the value of "x" in the outer scope. When "foo" is called, it results the value of "x" in the outer scope, which is 5. The result of "console.log(x)" is therefore 5.
85. What is the result of the following code: console.log(typeof NaN);
Explanation:
In JavaScript, NaN (Not a Number) is a special value of the number data type that represents an undefined or unrepresentable value resulting from a mathematical operation. Despite its name, NaN is still considered a number data type. Therefore, the result of "console.log(typeof NaN)" is "number".
86. What is the result of the following code:
var x = [1, 2, 3]; var y = x.slice(0, 2); console.log(y);
Answer:
Explanation:
In JavaScript, the "slice" method is used to extract a portion of an array into a new array. It takes two arguments: the starting index and the ending index (exclusive) of the slice. In this case, "x.slice(0, 2)" extracts the elements at indices 0 and 1 (i.e. [1, 2]) into a new array, which is assigned to "y". Therefore, the result of "console.log(y)" is [1, 2].
87. What is the result of the following code: console.log("3" + 2);
Explanation:
In JavaScript, the "+" operator is used for both addition and concatenation. When used with a string and a number, it performs concatenation rather than addition. Therefore, the result of "console.log("3" + 2)" is "32".
88. What is the result of the following code: console.log(null == undefine
Explanation:
In JavaScript, null and undefined are equal to each other and to no other value. Therefore, the result of "console.log(null == undefined" is true.
89. What is the result of the following code:
var x = 5;
function foo() {
console.log(x);
} foo();
Answer:
Explanation:
In this code, the variable "x" is declared in the global scope and assigned the value 5. The function "foo" is defined to log the value of "x". When "foo" is called, it results the value of "x" in the outer scope, which is 5. Therefore, the result of "console.log(x)" is 5.
90. What is the result of the following code: console.log(0.1 + 0.2 == 0.3);
Explanation:
In JavaScript, floating-point numbers are represented using binary fractions, which can sometimes result in rounding errors. Therefore, the expression "0.1 + 0.2" does not equal exactly 0.3, but instead equals a slightly larger number (e.g. 0.30000000000000004). Therefore, the result of "console.log(0.1 + 0.2 == 0.3)" is false.
91. What is the result of the following code:
var x = {name: "John", age: 30};
console.log(Object.keys(x));
Answer:
Explanation:
In JavaScript, the "Object.keys" method is used to retrieve an array of an object's own enumerable property names. In this case, "Object.keys(x)" returns an array containing the property names "name" and "age". Therefore, the result of "console.log(Object.keys(x))" is ["name", "age"].
92. What is the result of the following code: console.log(typeof NaN);
Explanation:
In JavaScript, NaN (Not-a-Number) is a special value of the number type. Therefore, the result of "console.log(typeof NaN)" is "number".
93. What is the result of the following code: console.log("foo".indexOf("o"));
Answer:
Explanation:
In JavaScript, the "indexOf" method is used to find the index of the first occurrence of a substring within a string. In this case, "foo" contains two occurrences of "o", but "indexOf" returns the index of the first occurrence, which is 1. Therefore, the result of "console.log("foo".indexOf("o"))" is 1.
94. What is the result of the following code:
var x = 5; var y = x++; console.log(x, y);
Explanation:
In this code, the variable "x" is assigned the value 5 and then incremented using the "++" operator. The variable "y" is assigned the original value of "x" (i.e. 5) before the incrementation. Therefore, the result of "console.log(x, y)" is 6, 5.
95. What is the result of the following code:
var x = ["a", "b", "c"]; x.splice(1, 1); console.log(x);
Answer:
Explanation:
In JavaScript, the "splice" method is used to add or remove elements from an array. In this case, "x.splice(1, 1)" removes one element starting from the index 1 (i.e. "b") from the array "x". Therefore, the result of "console.log(x)" is ["a", "c"].
96. What is the result of the following code: console.log(10 < 9 < 8);
Answer:
Explanation:
In JavaScript, the "<" operator performs a numerical comparison between two values. Therefore, the expression "10 < 9 < 8" is evaluated as "(10 < 9) < 8", which first evaluates "10 < 9" to false and then "false < 8" to true. Therefore, the result of "console.log(10 < 9 < 8)" is true.
97. What is the result of the following code:
var x = [1, 2, 3];
var y = x.map(function(n) { return n * 2; });
console.log(y);
Answer:
Explanation:
In JavaScript, the "map" method is used to create a new array by applying a function to each element of an existing array. In this case, the "map" method is used to create a new array "y" where each element is twice the value of the corresponding element in the original array "x". Therefore, the result of "console.log(y)" is [2, 4, 6].
98. What is the result of the following code: console.log(typeof null);
Answer:
Explanation:
In JavaScript, the "typeof" operator returns the data type of a value. However, there is a known bug where "typeof null" returns "object" instead of "null". Therefore, the result of "console.log(typeof null)" is "object".
99. What is the result of the following code:
var x = 5; var y = "5"; console.log(x == y);
Answer:
Explanation:
In JavaScript, the "==" operator performs type coercion before comparison. In this case, the number 5 and the string "5" are coerced to the same type (i.e. number) before comparison. Therefore, the result of "console.log(x == y)" is true.
100. What is the result of the following code:
var x = 5; var y = "5"; console.log(x === y);
Explanation:
In JavaScript, the "===" operator performs strict comparison without type coercion. In this case, the number 5 and the string "5" are of different types, so strict comparison will always return false. Therefore, the result of "console.log(x === y)" is false.
101. What is the result of the following code:
var x = [1, 2, 3]; console.log(x.toString());
Answer:
Explanation:
In JavaScript, the "toString" method is used to convert an array to a string by concatenating its elements with commas. Therefore, the result of "console.log(x.toString())" is "1,2,3".
102. What is the result of the following code:
var x = 5; var y = 3; console.log(x %= y);
Answer:
Explanation:
In JavaScript, the %= operator is the modulus assignment operator, which computes the modulus of two operands and assigns the result to the left operand. Therefore, x %= y is equivalent to x = x % y, which evaluates to 2. The result of console.log(x %= y) is 2.
103. What is the result of the following code: console.log(typeof []);
Answer:
Explanation:
In JavaScript, an array is a special type of object that can hold a collection of values. Therefore, the typeof operator returns "object" when applied to an array.
104. What is the result of the following code:
var x = [1, 2, 3]; x.push(4); console.log(x);
Answer:
Explanation:
In JavaScript, the push() method adds one or more elements to the end of an array and returns the new length of the array. Therefore, after x.push(4), the array x contains the elements [1, 2, 3, 4]. The result of console.log(x) is [1, 2, 3, 4].
105. What is the result of the following code:
var x = "hello"; var y = x.toUpperCase(); console.log(y);
Answer:
Explanation:
In JavaScript, the toUpperCase() method is used to convert a string to uppercase letters. Therefore, after y = x.toUpperCase(), the value of y is "HELLO". The result of console.log(y) is "HELLO".
106. What is the result of the following code:
var x = 5; var y = "3"; console.log(x + y);
Answer:
Explanation:
In JavaScript, the + operator can be used for addition or string concatenation, depending on the type of the operands. If either operand is a string, the + operator performs string concatenation. In this case, the value of x is a number and the value of y is a string, so the + operator performs string concatenation and the result of console.log(x + y) is "53".
107. What is the result of the following code:
var x = 10; var y = 5; console.log((x > y) && (y < 6));
Answer:
Explanation:
In JavaScript, the && operator returns true if both operands are true, and false otherwise. In this case, (x > y) evaluates to true (because 10 is greater than 5), but (y < 6) evaluates to false (because 5 is not less than 6). Therefore, the expression (x > y) && (y < 6) evaluates to false. The result of console.log((x > y) && (y < 6)) is false.
108. What is the result of the following code:
var x = "5"; var y = "3"; console.log(x + y);
Answer:
Explanation:
In JavaScript, the + operator performs string concatenation when either operand is a string. In this case, both x and y are strings, so the + operator performs string concatenation and the result of console.log(x + y) is "53".
109. What is the result of the following code:
var x = 10; var y = "5"; console.log(x + y);
Answer:
Explanation:
In JavaScript, the + operator performs string concatenation when either operand is a string. In this case, y is a string and x is a number, so the + operator performs string concatenation and the result of console.log(x + y) is "105".
110. What is the result of the following code:
var x = [1, 2, 3]; var y = [1, 2, 3]; console.log(x == y);
Answer:
Explanation:
In JavaScript, arrays are objects, and two arrays are considered equal only if they refer to the same object. In this case, x and y are two different arrays with the same elements, so the expression x == y evaluates to false. The result of console.log(x == y) is false.
111. What is the result of the following code:
var x = 10; var y = 3; console.log(x / y);
Answer:
Explanation:
In JavaScript, the / operator performs division of two operands. When one or both operands are floating-point numbers, the result is also a floating-point number. Therefore, the expression x / y evaluates to 3.33333... (with repeating decimals), but the result of console.log(x / y) is rounded to two decimal places and displayed as 3.33.
112. Which of the following is not a primitive data type in JavaScript?
Answer:
Explanation:
In JavaScript, primitive data types are String, Number, Boolean, null, undefined, and symbol. Array is not a primitive data type; it is an object type.
113. Which of the following methods removes the last element from an array and returns that element?
Answer:
Explanation:
The pop() method removes the last element from an array and returns that element. The push() method adds one or more elements to the end of an array, while the shift() method removes the first element from an array and returns that element. The unshift() method adds one or more elements to the beginning of an array.
114. What is the result of the following code?
function foo() {
var a = b = 3;
}
foo();
console.log("a defined? " + (typeof a !==
'undefined'));
console.log("b defined? " + (typeof b !==
'undefined'));
Answer:
Explanation:
The function foo() declares a variable a and assigns it the value of 3, but it also assigns the value of 3 to an undeclared variable b. This creates a global variable b with a value of 3, but a is only defined within the scope of the function. Therefore, typeof a !== 'undefined' will be true within the function, but false outside of it. typeof b !== 'undefined' will be true because it is a global variable.