Test your JavaScript Skills 50 Questions


Question 1

What will be the output of the following code?

console.log(typeof null);

A) “null”
B) “undefined”
C) “object”
D) “number”

Answer: C) “object”

Explanation: In JavaScript, typeof null returns “object”. This is a well-known bug in JavaScript that exists for legacy reasons.


Question 2

What is the output of the following code?

console.log(0 == ‘0’);

A) true
B) false
C) Error
D) Undefined

Answer: A) true

Explanation: The == operator performs type coercion, so 0 and ‘0’ are considered equal.


Question 3

Which method can be used to convert a JSON text into a JavaScript object? A) JSON.stringify()
B) JSON.parse()
C) JSON.toString()
D) JSON.object()

Answer: B) JSON.parse()

Explanation: JSON.parse() converts a JSON string into a JavaScript object.


Question 4

What will the following code output?

console.log(1 + ‘2’ + ‘2’);

A) “32”
B) “122”
C) 5
D) “14”

Answer: B) “122”

Explanation: JavaScript concatenates strings from left to right, so 1 + ‘2’ becomes ’12’ and ’12’ + ‘2’ becomes ‘122’.


Question 5

What will be the output of the following code?

console.log(‘2’ – 1);

A) “21”
B) “1”
C) 1
D) NaN

Answer: C) 1

Explanation: The – operator forces JavaScript to coerce ‘2’ into a number, so the result is 2 – 1 which is 1.


Question 6

Which of the following is not a primitive data type in JavaScript? A) String
B) Number
C) Boolean
D) Object

Answer: D) Object

Explanation: Object is not a primitive data type. The primitive data types are String, Number, Boolean, Null, Undefined, and Symbol.


Question 7

What is the result of the following code?

var foo = function() {

  return bar();

  var bar = function() {

    return 3;

  };

  function bar() {

    return 8;

  }

}

console.log(foo());

A) 3
B) 8
C) Error
D) Undefined

Answer: B) 8

Explanation: Function declarations are hoisted above variable declarations and assignments. The function bar defined by function bar() { return 8; } is hoisted.


Question 8

What will be the output of the following code?

let x = {};

let y = x;

console.log(x === y);

y.z = 1;

console.log(x.z);

A) true, undefined
B) false, 1
C) true, 1
D) false, undefined

Answer: C) true, 1

Explanation: Objects are compared by reference, so x and y are referencing the same object. Therefore, x === y is true and adding a property to y reflects in x.


Question 9

What is the result of the following code?

console.log([] + []);

A) “”
B) []
C) “[]”
D) Undefined

Answer: A) “”

Explanation: When you use the + operator with arrays, JavaScript converts them to strings. An empty array becomes an empty string, so [] + [] results in “”.


Question 10

Which of the following methods is used to add elements to the end of an array? A) push()
B) pop()
C) shift()
D) unshift()

Answer: A) push()

Explanation: push() adds one or more elements to the end of an array and returns the new length of the array.


Question 11

What is the output of the following code?

console.log(typeof NaN);

A) “number”
B) “NaN”
C) “undefined”
D) “object”

Answer: A) “number”

Explanation: NaN is a special value representing “Not-a-Number” in JavaScript, but its type is still “number”.


Question 12

What will be the output of the following code?

console.log(‘5’ – ‘2’);

A) “52”
B) 3
C) “3”
D) NaN

Answer: B) 3

Explanation: The – operator converts both strings to numbers and then performs the subtraction.


Question 13

Which of the following statements will throw an error? A) let x = 10;
B) var y = 20;
C) const z = 30;
D) x = 40;

Answer: D) x = 40;

Explanation: The statement x = 40; will throw an error if x was declared with const, but as shown it doesn’t declare a variable in this context. However, this is a poorly worded question; all shown variable declarations are correct.


Question 14

What will the following code output?

console.log(1 + true);

A) “1true”
B) 2
C) 1
D) NaN

Answer: B) 2

Explanation: true is converted to 1, so 1 + true becomes 1 + 1, which is 2.


Question 15

What is the purpose of the bind() method in JavaScript? A) It creates a new function that, when called, has its this keyword set to the provided value.
B) It calls a function with a given this value and arguments provided individually.
C) It calls a function with a given this value and arguments provided as an array.
D) It adds properties to an object.

Answer: A) It creates a new function that, when called, has its this keyword set to the provided value.

Explanation: The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.


Question 16

Which of the following is not a looping structure in JavaScript? A) for
B) while
C) do-while
D) foreach

Answer: D) foreach

Explanation: foreach is not a native looping structure in JavaScript; it is a method of arrays. The native loops are for, while, and do-while.


Question 17

What will be the output of the following code?

console.log(‘5’ + 5);

A) 10
B) “55”
C) 5
D) NaN

Answer: B) “55”

Explanation: The + operator concatenates the string ‘5’ with the number 5, resulting in the string “55”.


Question 18

What is the output of the following code?

console.log(0.1 + 0.2 === 0.3);

A) true
B) false
C) Undefined
D) Error

Answer: B) false

Explanation: Due to floating-point precision, 0.1 + 0.2 does not exactly equal 0.3. It equals 0.30000000000000004.


Question 19

What does the Array.prototype.map() method do? A) It mutates the original array.
B) It executes a provided function once for each array element.
C) It creates a new array with the results of calling a provided function on every element in the calling array.
D) It sorts the array elements.

Answer: C) It creates a new array with the results of calling a provided function on every element in the calling array.

Explanation: The map() method creates a new array populated with the results of calling a provided function on every element in the calling array without modifying the original array.


Question 20

What is the result of the following code?

console.log([10, 20, 30].reduce((acc, val) => acc + val, 0));

A) “102030”
B) 60
C) “60”
D) [10, 20, 30]

Answer: B) 60

Explanation: The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. Here, it sums the values, resulting in 60.


Question 21

Which statement is used to stop a loop in JavaScript? A) break
B) stop
C) halt
D) exit

Answer: A) break

Explanation: The break statement is used to terminate the current loop, switch, or label statement.


Question 22

What will be the output of the following code?

console.log(typeof undefined);

A) “null”
B) “undefined”
C) “object”
D) “number”

Answer: B) “undefined”

Explanation: typeof undefined returns “undefined” as undefined is a primitive value in JavaScript.


Question 23

Which method is used to add one or more elements to the beginning of an array? A) push()
B) pop()
C) shift()
D) unshift()

Answer: D) unshift()

Explanation: The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.


Question 24

What will be the output of the following code?

console.log(Boolean(”));

A) true
B) false
C) “false”
D) undefined

Answer: B) false

Explanation: An empty string ” is a falsy value in JavaScript, so Boolean(”) returns false.


Question 25

What does the typeof operator do? A) It returns the type of a variable.
B) It returns the value of a variable.
C) It checks if a variable is defined.
D) It checks if a variable is an array.

Answer: A) It returns the type of a variable.

Explanation: The typeof operator returns a string indicating the type of the unevaluated operand.


Question 26

What will the following code output?

let a = [1, 2, 3];

let b = a;

b.push(4);

console.log(a);

A) [1, 2, 3]
B) [1, 2, 3, 4]
C) [1, 2, 3, 4, 4]
D) Error

Answer: B) [1, 2, 3, 4]

Explanation: Both a and b reference the same array, so changes to b are reflected in a.


Question 27

What is the output of the following code?

console.log(1 + “2” + 3);

A) “6”
B) “123”
C) 123
D) “15”

Answer: B) “123”

Explanation: JavaScript performs string concatenation from left to right, so 1 + “2” becomes “12” and “12” + 3 becomes “123”.


Question 28

Which method is used to remove the first element from an array? A) push()
B) pop()
C) shift()
D) unshift()

Answer: C) shift()

Explanation: The shift() method removes the first element from an array and returns that removed element.


Question 29

What will be the output of the following code?

console.log(typeof NaN);

A) “NaN”
B) “undefined”
C) “object”
D) “number”

Answer: D) “number”

Explanation: NaN stands for “Not-a-Number” but its type is “number” in JavaScript.


Question 30

Which statement is used to handle exceptions in JavaScript? A) handle
B) catch
C) try
D) except

Answer: C) try

Explanation: The try…catch statement marks a block of statements to try and specifies a response should an exception be thrown.


Question 31

What will be the output of the following code?

console.log(10 === ’10’);

A) true
B) false
C) “true”
D) “false”

Answer: B) false

Explanation: The === operator checks for both value and type equality, so 10 (number) is not equal to ’10’ (string).


Question 32

Which of the following methods is used to join two or more arrays? A) concat()
B) join()
C) merge()
D) link()

Answer: A) concat()

Explanation: The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.


Question 33

What is the output of the following code?

console.log([1, 2, 3] instanceof Array);

A) true
B) false
C) “true”
D) “false”

Answer: A) true

Explanation: The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor. Arrays are instances of the Array constructor.


Question 34

What will be the output of the following code?

console.log(‘Hello’.toUpperCase());

A) HELLO
B) hello
C) “HELLO”
D) “hello”

Answer: C) “HELLO”

Explanation: The toUpperCase() method returns the calling string value converted to uppercase.


Question 35

What is the purpose of the Object.keys() method? A) To return an array of the object’s property names.
B) To return an array of the object’s values.
C) To create a new object with the specified prototype.
D) To merge two or more objects.

Answer: A) To return an array of the object’s property names.

Explanation: The Object.keys() method returns an array of a given object’s own enumerable property names.


Question 36

What will the following code output?

console.log(‘5’ – 3);

A) 2
B) “2”
C) “53”
D) NaN

Answer: A) 2

Explanation: The – operator converts the string ‘5’ to a number, resulting in 5 – 3, which equals 2.


Question 37

Which method can be used to create a new array with all elements that pass a test implemented by the provided function? A) map()
B) filter()
C) reduce()
D) every()

Answer: B) filter()

Explanation: The filter() method creates a new array with all elements that pass the test implemented by the provided function.


Question 38

What is the result of the following code?

let a = 5;

let b = 10;

[a, b] = [b, a];

console.log(a, b);

A) 5, 10
B) 10, 5
C) undefined
D) Error

Answer: B) 10, 5

Explanation: The array destructuring assignment swaps the values of a and b.


Question 39

Which of the following is not a primitive data type in JavaScript? A) String
B) Number
C) Boolean
D) Array

Answer: D) Array

Explanation: Arrays are not primitive data types; they are objects.


Question 40

What will be the output of the following code?

console.log(typeof function() {});

A) “object”
B) “function”
C) “undefined”
D) “boolean”

Answer: B) “function”

Explanation: The typeof operator returns “function” for functions.


Question 41

Which method removes the last element from an array and returns that element? A) pop()
B) push()
C) shift()
D) unshift()

Answer: A) pop()

Explanation: The pop() method removes the last element from an array and returns that element.


Question 42

What is the result of the following code?

console.log(’10’ * 2);

A) 20
B) “102”
C) NaN
D) “20”

Answer: A) 20

Explanation: The * operator converts the string ’10’ to a number and performs the multiplication, resulting in 20.


Question 43

Which operator is used to assign a value to a variable? A) *
B) =
C) ==
D) ===

Answer: B) =

Explanation: The = operator is used to assign a value to a variable.


Question 44

What will be the output of the following code?

console.log([1, 2, 3].length);

A) 3
B) 2
C) 1
D) undefined

Answer: A) 3

Explanation: The length property of an array returns the number of elements in that array.


Question 45

Which method is used to remove whitespace from both ends of a string? A) slice()
B) substr()
C) trim()
D) strip()

Answer: C) trim()

Explanation: The trim() method removes whitespace from both ends of a string.


Question 46

What will be the output of the following code?

console.log([1, 2, 3].map(x => x * x));

A) [1, 4, 9]
B) [1, 2, 3]
C) [1, 8, 27]
D) [2, 3, 4]

Answer: A) [1, 4, 9]

Explanation: The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.


Question 47

Which method is used to create a shallow copy of a portion of an array into a new array object? A) copy()
B) slice()
C) splice()
D) concat()

Answer: B) slice()

Explanation: The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included).


Question 48

What is the result of the following code?

let a = 1;

let b = 2;

let c = a++;

console.log(a, b, c);

A) 2, 2, 2
B) 2, 2, 1
C) 1, 2, 2
D) 1, 2, 1

Answer: B) 2, 2, 1

Explanation: The a++ operator increments a but returns the original value of a, which is 1. Therefore, c is 1, and a is incremented to 2.


Question 49

Which of the following is a correct way to create a new object in JavaScript? A) var obj = {};
B) var obj = new Object();
C) var obj = Object.create(null);
D) All of the above

Answer: D) All of the above

Explanation: All the methods shown are correct ways to create a new object in JavaScript.


Question 50

What is the result of the following code?

console.log(’10’ – ‘4’);

A) 6
B) “104”
C) NaN
D) “6”

Answer: A) 6

Explanation: The – operator converts both strings to numbers and then performs the subtraction, resulting in 6.