an HCL GUVI product

javascript programming banner

JavaScript Multiple Choice Questions (MCQs) and Answers

Master JavaScript with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of JavaScript. Begin your placement preparation journey now!

Q1

Q1 What is the purpose of JavaScript in web development?

A

To structure web pages

B

To style web pages

C

To add interactivity and dynamic content to web pages

D

To store data on the server

Q2

Q2 Which keyword is used for declaring a variable in JavaScript that can be reassigned?

A

const

B

var

C

let

D

static

Q3

Q3 In JavaScript, which of the following is a valid variable name?

A

2names

B

$name

C

-name

D

name2

Q4

Q4 Which data type in JavaScript is used to represent logical values?

A

String

B

Boolean

C

Number

D

Undefined

Q5

Q5 What does the undefined value in JavaScript represent?

A

An unassigned variable

B

A null value

C

A logical false

D

An error condition

Q6

Q6 What will be the output of the following code?
console.log(typeof null);

A

'object'

B

'null'

C

'undefined'

D

'number'

Q7

Q7 What is the output of the following code snippet?
var a = 10;
console.log(a);

A

10

B

'10'

C

undefined

D

null

Q8

Q8 Which statement is used to execute a block of code multiple times in JavaScript?

A

for

B

if

C

return

D

break

Q9

Q9 What does the if statement in JavaScript do?

A

Declares a variable

B

Executes a block of code based on a condition

C

Prints a message to the console

D

Loops through a block of code

Q10

Q10 Which of the following is not a loop structure in JavaScript?

A

while

B

for

C

if

D

do-while

Q11

Q11 In a switch statement, what keyword is used to terminate a case in JavaScript?

A

end

B

break

C

stop

D

exit

Q12

Q12 What will be the output of the following code?
let a = 2;
if(a > 3) {
console.log('Yes');
} else {
console.log('No');
}

A

Yes

B

No

C

Undefined

D

Error

Q13

Q13 In a for loop, what are the three optional expressions, separated by semicolons?

A

Initializer, Condition, Incrementer

B

Condition, Incrementer, Initializer

C

Incrementer, Initializer, Condition

D

Condition, Initializer, Incrementer

Q14

Q14 Identify the problem in this code:
let i = 0;
while (i < 3) {
console.log(i);
}

A

Infinite loop

B

Syntax error

C

Logical error

D

No output

Q15

Q15 What is the result of trying to extend the length of an array using a function in JavaScript?
function extendArray(arr) {
arr.push(5);
}
let myArr = [1, 2, 3];
extendArray(myArr);
console.log(myArr.length);

A

3

B

4

C

5

D

Error

Q16

Q16 What does the following function return?
function checkEven(number) {
return number % 2 === 0;
}
console.log(checkEven(3));

A

true

B

false

C

3

D

Error

Q17

Q17 In JavaScript, how can you check if a variable is an array?

A

typeof variable

B

variable.isArray()

C

Array.isArray(variable)

D

variable instanceof Array

Q18

Q18 What is the output of the following code?
let fruits = ['apple', 'banana', 'mango'];
console.log(fruits[1]);

A

apple

B

banana

C

mango

D

undefined

Q19

Q19 Consider the following code:
let arr = [1, 2, 3];
arr[5] = 5;
console.log(arr.filter(x => x === undefined).length);
What is the output?

A

0

B

2

C

3

D

5

Q20

Q20 Spot the mistake in this code snippet:
let data = [1, 2, 3];
delete data[1];
console.log(data[1]);

A

undefined is logged instead of 2

B

2 is not deleted

C

Syntax error

D

No error

Q21

Q21 What will be the output of console.log(typeof {})?

A

'object'

B

'array'

C

'null'

D

'undefined'

Q22

Q22 In JavaScript, what is a method?

A

A predefined function

B

A loop inside an object

C

A function stored as an object property

D

An external library function

Q23

Q23 How do you create a new object in JavaScript?

A

Object.create()

B

new Object()

C

Both A and B

D

Neither A nor B

Q24

Q24 What is the purpose of the this keyword in JavaScript objects?

A

Refer to the global object

B

Refer to the current object

C

Create a new object

D

Duplicate an object

Q25

Q25 What is the result of accessing a property that doesn’t exist on an object?

A

null

B

undefined

C

0

D

Error

Q26

Q26 What is the output of the following code snippet?
let obj = { a: 1, b: 2 };
console.log(obj.a);

A

1

B

2

C

undefined

D

Error

Q27

Q27 Consider the code:
let person = { name: 'Alice', age: 25 };
delete person.age; console.log(person.age);
What is the output?

A

'Alice'

B

25

C

undefined

D

Error

Q28

Q28 Given the following code, what is printed to the console?
let obj1 = { a: 1 };
let obj2 = { a: 1 };
console.log(obj1 === obj2);

A

true

B

false

C

null

D

Error

Q29

Q29 What does DOM stand for in web development?

A

Document Object Model

B

Data Object Management

C

Direct Object Manipulation

D

Display Object Model

Q30

Q30 How can you change the text content of an HTML element in the DOM using JavaScript?

A

element.textContent = 'new text'

B

element.innerHTML = 'new text'

C

Both A and B

D

Neither A nor B