Debug School

Akanksha
Akanksha

Posted on

Top 30 JavaScript Interview Questions with Answers

1. What is JavaScript?

a. A server-side scripting language
b. A client-side scripting language
c. Both a and b
d. None of the above
Answer: b. A client-side scripting language

2. Which of the following is a primitive data type in JavaScript?

a. Object
b. String
c. Function
d. Array
Answer: b. String

3. What is the output of typeof null in JavaScript?

a. 'object'
b. 'null'
c. 'undefined'
d. 'string'
Answer: a. 'object'

4. Which keyword is used to declare variables in JavaScript?

a. let
b. var
c. const
d. all of the above
Answer: d. all of the above

5. What will the following code output: console.log(2 + '2');

a. 4
b. '22'
c. '4'
d. 22
Answer: b. '22'

6. Which method is used to add elements to the end of an array in JavaScript?

a. push()
b. unshift()
c. add()
d. append()
Answer: a. push()

7. What does the 'isNaN()' function do in JavaScript?

a. Returns true if the argument is not a number
b. Returns true if the argument is a number
c. Returns false if the argument is not a number
d. Returns false if the argument is a number
Answer: a. Returns true if the argument is not a number

8. Which event is triggered when a user clicks on an HTML element in JavaScript?

a. onclick
b. onhover
c. onsubmit
d. onchange
Answer: a. onclick

9. What is the purpose of the JSON.parse() method in JavaScript?

a. Converts a JSON string to a JavaScript object
b. Converts a JavaScript object to a JSON string
c. Parses HTML content
d. None of the above
Answer: a. Converts a JSON string to a JavaScript object

10. What does the === operator do in JavaScript?

a. Checks for equality of values
b. Checks for equality of values and types
c. Checks for inequality of values
d. Checks for inequality of values and types
Answer: b. Checks for equality of values and types

11. What is the output of 3 + 2 + '7' in JavaScript?

a. 57
b. '57'
c. '32'
d. 32
Answer: c. '32'

12. Which function is used to set a timer in JavaScript?

a. setInterval()
b. setTimeout()
c. setTimer()
d. startTimer()
Answer: b. setTimeout()

13. What is a closure in JavaScript?

a. A function that has access to its own scope
b. A function that can access its outer function's scope
c. A function that has access to global scope
d. None of the above
Answer: b. A function that can access its outer function's scope

14. What is the purpose of the 'use strict' directive in JavaScript?

a. Forces the use of strict typing
b. Enforces stricter parsing and error handling
c. Allows for relaxed rules in coding
d. None of the above
Answer: b. Enforces stricter parsing and error handling

15. Which method is used to remove the last element from an array in JavaScript?

a. pop()
b. shift()
c. remove()
d. slice()
Answer: a. pop()

16. Which function is used to convert a string to lowercase in JavaScript?

a. toLowerCase()
b. toLower()
c. lowerCase()
d. caseLower()
Answer: a. toLowerCase()

17. What is the purpose of the 'try...catch' statement in JavaScript?

a. Executes code inside 'try' and handles errors in 'catch'
b. Executes code inside 'catch' and handles errors in 'try'
c. Executes code inside both 'try' and 'catch'
d. None of the above
Answer: a. Executes code inside 'try' and handles errors in 'catch'

18. Which method is used to join elements of an array into a string in JavaScript?

a. join()
b. concat()
c. merge()
d. concatenate()
Answer: a. join()

19. What is the purpose of the 'break' statement in a loop in JavaScript?

a. Terminates the loop and transfers control to the next iteration
b. Exits the entire loop
c. Skips the current iteration and proceeds to the next iteration
d. None of the above
Answer: a. Terminates the loop and transfers control to the next iteration

20. What does the decodeURI() function do in JavaScript?

a. Decodes a Uniform Resource Identifier (URI)
b. Encodes a URI
c. Decodes a URL
d. Encodes a URL
Answer: a. Decodes a Uniform Resource Identifier (URI)

21. What is the output of typeof undefined in JavaScript?

a. 'undefined'
b. 'null'
c. 'object'
d. 'string'
Answer: a. 'undefined'

22. Which method is used to split a string into an array of substrings based on a specified delimiter in JavaScript?

a. split()
b. slice()
c. divide()
d. separate()
Answer: a. split()

23. Which keyword is used to declare a function in JavaScript?

a. func
b. function
c. def
d. define
Answer: b. function

24. What is the purpose of the 'continue' statement in a loop in JavaScript?

a. Exits the loop
b. Skips the current iteration and proceeds to the next iteration
c. Terminates the loop and transfers control to the next iteration
d. None of the above
Answer: b. Skips the current iteration and proceeds to the next iteration

25. What is the output of parseInt('10.123') in JavaScript?

a. 10
b. 10.123
c. NaN
d. 0
Answer: a. 10

26. What does the 'trim()' method do in JavaScript?

a. Removes whitespace from both ends of a string
b. Adds whitespace to both ends of a string
c. Converts the string to lowercase
d. None of the above
Answer: a. Removes whitespace from both ends of a string

27. What is the purpose of the 'call()' method in JavaScript?

a. Calls a function with a given 'this' value and arguments provided individually
b. Calls a function with a given 'this' value and arguments provided as an array
c. Calls a function without any arguments
d. None of the above
Answer: a. Calls a function with a given 'this' value and arguments provided individually

28. What is the output of typeof NaN in JavaScript?

a. 'number'
b. 'NaN'
c. 'undefined'
d. 'Not a Number'
Answer: a. 'number'

29. Which operator is used for exponentiation in JavaScript?

a. ^
b. **
c. **
d. ^^
Answer: b. **

30. What is the output of 0 == '0' in JavaScript?

a. true
b. false
c. undefined
d. null
Answer: a. true

31. What is the purpose of the 'Math.random()' function in JavaScript?

a. Generates a random number between 0 (inclusive) and 1 (exclusive)
b. Generates a random integer between 0 and a specified number
c. Generates a random number between 0 and a specified number
d. None of the above
Answer: a. Generates a random number between 0 (inclusive) and 1 (exclusive)

Top comments (0)