JavaScript MCQs – JavaScript Basics

JavaScript basics are essential for building a strong foundation in web development. Practicing multiple-choice questions (MCQs) on topics like variables, data types, functions, loops, and operators helps reinforce your understanding of JavaScript’s core concepts. By consistently practicing JavaScript basics MCQs, you can improve your problem-solving skills and gain confidence in applying the language effectively in both simple and complex scenarios.

1.) How do you declare a variable in JavaScript?

A) var
B) let
C) const
D) All of the above

Answer: Option D

Explanation: JavaScript allows you to declare variables using var, let, and const. var is function-scoped, while let and const are block-scoped.

2.) What is the default value of a declared variable that has not been assigned a value?

A) null
B) undefined
C) 0
D) NaN

Answer: Option B

Explanation: Variables declared without a value in JavaScript are automatically assigned the value undefined.

3.) Which of the following is not a valid JavaScript variable name?

A) 2name
B) _name
C) $name
D) name2

Answer: Option A

Explanation: Variable names in JavaScript cannot start with a number.

4.) How can you write a multi-line comment in JavaScript?

A) <!– Comment –>
B) // Comment
C) /* Comment */
D) /* Comment // Comment */

Answer: Option C

Explanation: Multi-line comments in JavaScript are written using /* */.

5.) Which method is used to add an element to the end of an array?

A) push()
B) pop()
C) unshift()
D) splice()

Answer: Option A

Explanation: The push() method adds an element to the end of an array.

6.) What does the isNaN() function do?

A) Checks if a value is a number.
B) Checks if a value is NaN.
C) Converts a string to a number.
D) None of the above.

Answer: Option B

Explanation: isNaN() checks whether a value is NaN (Not a Number).

7.) Which of the following methods correctly copies an array in JavaScript?

A) let newArr = oldArr;
B) let newArr = […oldArr];
C) let newArr = oldArr.slice();
D) Both B and C

Answer: Option D

Explanation: Both the spread operator ([…]) and the slice() method create shallow copies of arrays. Option A does not create a copy; it assigns a reference.

8.) How do you convert a string to an integer in JavaScript?

A) Number.parseInt(“42”)
B) nt(“42”)
C) parseInteger(“42”)
D) convertToInt(“42”)

Answer: Option A

Explanation: The parseInt() function is used to convert a string to an integer.

9.) Which operator is used to check equality of value and type?

A) =
B) ==
C) ===
D) !=

Answer: Option C

Explanation: The === operator checks for both value and type equality.

10.) Which statement correctly defines a function in JavaScript?

A) def myFunction() {}
B) function myFunction() {}
C) function: myFunction() {}
D) fun myFunction() {}

Answer: Option B

Explanation: The correct syntax to define a function in JavaScript is function myFunction() {}.

Leave a Reply

Your email address will not be published. Required fields are marked *