Top 50+ JavaScript Interview MCQS for 2024

 


 

 1. Which of the following is a correct way to declare a variable in JavaScript?

    A. variable x = 10;

    B. var x = 10;

    C. dim x = 10;

    D. int x = 10;

    Answer: B. var x = 10;

 

 2. Which of the following is not a valid JavaScript data type?

    A. String

    B. Boolean

    C. Float

    D. Object

    Answer: C. Float

 

 3. How do you create a function in JavaScript?

    A. function myFunction()

    B. def myFunction()

    C. function:myFunction()

    D. create myFunction()

    Answer: A. function myFunction()

 

 4. Which builtin method sorts the elements of an array?

    A. changeOrder(order)

    B. order()

    C. sort()

    D. arrange()

    Answer: C. sort()

 

 5. What is the correct syntax for referring to an external script called "script.js"?

    A. <script href="script.js">

    B. <script name="script.js">

    C. <script src="script.js">

    D. <script file="script.js">

    Answer: C. <script src="script.js">

 

 6. How do you write "Hello World" in an alert box?

    A. msg("Hello World");

    B. alertBox("Hello World");

    C. msgBox("Hello World");

    D. alert("Hello World");

    Answer: D. alert("Hello World");

 

 7. How do you call a function named "myFunction"?

    A. call myFunction()

    B. call function myFunction()

    C. myFunction()

    D. Call.myFunction()

    Answer: C. myFunction()

 

 8. Which event occurs when the user clicks on an HTML element?

    A. onchange

    B. onclick

    C. onmouseclick

    D. onmouseover

    Answer: B. onclick

 

 9. How do you declare a JavaScript variable?

    A. var carName;

    B. variable carName;

    C. v carName;

    D. string carName;

    Answer: A. var carName;

 

 10. Which operator is used to assign a value to a variable?

    A. *

    B. -

    C. =

    D. x

    Answer: C. =

 

 11. What is the correct JavaScript syntax to change the content of the HTML element below?

   <p id="demo">This is a demonstration.</p>

  

    A. document.getElementById("demo").innerHTML = "Hello World!";

    B. document.getElementByName("p").innerHTML = "Hello World!";

    C. document.getElement("p").innerHTML = "Hello World!";

    D. demo.innerHTML = "Hello World!";

    Answer: A. document.getElementById("demo").innerHTML = "Hello World!";

 

 12. How do you write an IF statement in JavaScript?

    A. if i == 5 then

    B. if i = 5 then

    C. if (i == 5)

    D. if i = 5

    Answer: C. if (i == 5)

 

 13. How does a FOR loop start?

    A. for (i <= 5; i++)

    B. for (i = 0; i <= 5; i++)

    C. for (i = 0; i <= 5)

    D. for i = 1 to 5

    Answer: B. for (i = 0; i <= 5; i++)

 

 14. How can you add a comment in JavaScript?

    A. 'This is a comment

    B. <!This is a comment>

    C. //This is a comment

    D.  **This is a comment **h

    Answer: C. //This is a comment

 

 15. Which method removes the last element from an array and returns that element?

    A. pop()

    B. last()

    C. get()

    D. None of the above

    Answer: A. pop()

 

 16. How can you detect the client's browser name in JavaScript?

    A. browser.name

    B. client.navName

    C. navigator.appName

    D. browser.appName

    Answer: C. navigator.appName

 

 17. Which of the following is not a reserved word in JavaScript?

    A. interface

    B. throws

    C. program

    D. short

    Answer: C. program

 

 18. How do you find the number with the highest value of x and y in JavaScript?

    A. Math.max(x, y)

    B. Math.ceil(x, y)

    C. Math.floor(x, y)

    D. ceil(x, y)

    Answer: A. Math.max(x, y)

 

 19. Which of the following is a valid way to create an array in JavaScript?

    A. var arr = new Array();

    B. var arr = [];

    C. var arr = new Array(5);

    D. All of the above

    Answer: D. All of the above

 

 20. How do you round the number 7.25 to the nearest integer?

    A. rnd(7.25)

    B. Math.rnd(7.25)

    C. Math.round(7.25)

    D. round(7.25)

    Answer: C. Math.round(7.25)

 

 21. Which of the following is not a JavaScript framework or library?

    A. React

    B. Angular

    C. Vue

    D. Cassandra

    Answer: D. Cassandra

 

 22. How do you add a property to an object in JavaScript?

    A. object.property = value;

    B. object>property = value;

    C. object::property = value;

    D. object|property = value;

    Answer: A. object.property = value;

 

 23. Which operator is used to compare both value and type in JavaScript?

    A. ==

    B. ===

    C. !=

    D. !==

    Answer: B. ===

 

 24. What is the correct way to write a JavaScript array?

    A. var colors = "red", "green", "blue";

    B. var colors = 1 = ("red"), 2 = ("green"), 3 = ("blue");

    C. var colors = ["red", "green", "blue"];

    D. var colors = (1:"red", 2:"green", 3:"blue");

    Answer: C. var colors = ["red", "green", "blue"];

 

 25. How do you write a switch statement in JavaScript?

    A. switch x { case 1: break; case 2: break; }

    B. switch(x) { case 1: break; case 2: break; }

    C. switch(x) { case: 1; break; case: 2; break; }

    D. switch { x case 1: break; case 2: break; }

    Answer: B. switch(x) { case 1: break; case 2: break; }

 

 26. How do you 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


 27. Which method is used to convert a JSON text into a JavaScript object?

    A. JSON.parse()

    B. JSON.stringify()

    C. JSON.convert()

    D. JSON.toObject()

    Answer: A. JSON.parse()

 

 28. How do you check if a variable is an array in JavaScript?

    A. Array.isArray(variable)

    B. variable.isArray()

    C. typeof variable === 'array'

    D. variable instanceof Array

    Answer: A. Array.isArray(variable)

 

 29. What is the result of the following expression: "5" + 2?

    A. 52

    B. 7

    C. NaN

    D. undefined

    Answer: A. 52

 

 30. What will be the output of the following code?

   console.log(typeof NaN);


    A. number

    B. string

    C. object

    D. undefined

    Answer: A. number

 

 31. Which method is used to add elements to the end of an array?

    A. push()

    B. append()

    C. add()

    D. insert()

    Answer: A. push()

 

 32. What is the output of the following code?

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

  

    A. true

    B. false

    C. undefined

    D. NaN

    Answer: B. false

 

 33. Which method is used to remove whitespace from both ends of a string?

    A. trim()

    B. strip()

    C. cut()

    D. clear()

    Answer: A. trim()

 

 34. What is the purpose of the bind() method in JavaScript?

    A. To bind a function to an object.

    B. To call a function with a specific this value.

    C. To create a new function with the same body as an existing function.

    D. To change the context of a function.

    Answer: A. To bind a function to an object.

 

 35. How do you get the length of an array in JavaScript?

    A. array.length

    B. array.size

    C. array.count

    D. array.len

    Answer: A. array.length

 

 36. What will the following code output?

   console.log("Hello" === "hello");

  

    A. true

    B. false

    C. undefined

    D. NaN

    Answer: B. false

 

 37. How do you comment out a block of code in JavaScript?

    A. / Comment /

    B. <! Comment >

    C. // Comment //

    D. ( Comment )

    Answer: A. / Comment /

 

 38. What is the output of the following code?

   console.log(typeof null);

  

    A. object

    B. null

    C. undefined

    D. string

    Answer: A. object

 

 39. How do you create a date object in JavaScript?

    A. new Date()

    B. Date()

    C. create Date()

    D. create new Date()

    Answer: A. new Date()

 

 40. What does the isNaN function do?

    A. Checks if a value is null.

    B. Checks if a value is not a number.

    C. Checks if a value is undefined.

    D. Checks if a value is a number.

    Answer: B. Checks if a value is not a number.

 

 41. Which of the following is not a JavaScript loop structure?

    A. for

    B. foreach

    C. while

    D. dowhile

    Answer: B. foreach

 

 42. What does NaN stand for?

    A. Not a Number

    B. Not a Null

    C. Null and Not defined

    D. None of the Above

    Answer: A. Not a Number

 

 43. Which of the following is used to check if a property is in an object?

    A. in operator

    B. has operator

    C. exists operator

    D. None of the above

    Answer: A. in operator

 

 44. What will the following code output?

   console.log(typeof undefined == typeof NULL);

  

    A. true

    B. false

    C. undefined

    D. null

    Answer: A. true

 

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

    A. _myVar

    B. $myVar

    C. myvar

    D. myVar

    Answer: C. myvar

 

 46. What is the result of the following code?

   console.log("5"  3);

  

    A. 2

    B. 8

    C. NaN

    D. 53

    Answer: A. 2

 

 47. How do you create an 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

 

 48. What is the output of the following code?

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

  

    A. object

    B. function

    C. undefined

    D. null

    Answer: B. function

 

 49. Which method is used to remove the first element from an array and return that element?

    A. shift()

    B. unshift()

    C. pop()

    D. splice()

    Answer: A. shift()

 

 50. How do you add an element to the beginning of an array?

    A. push()

    B. unshift()

    C. shift()

    D. append()

    Answer: B. unshift()

0 Comments:

Post a Comment