Boost Your JavaScript Mastery: Dive into Our Interactive Quiz

JavaScript  QUIZ 25 Test your Knowledge!

  1. What will console.log(typeof undefined) output?
    • Explanation: The typeof operator in JavaScript is used to determine the type of a variable or a value. When used with undefined, it returns the string "undefined". This is because undefined is a primitive type in JavaScript representing the absence of a defined value.
  2. Which statement creates a constant in JavaScript?
    • Explanation: In JavaScript, the const keyword is used to declare a constant. A constant is a value that, once defined, cannot be reassigned. For example, const myConst = 10; creates a constant named myConst with the value 10.
  3. Which of these is a correct method to create a new array?
    • Explanation: All given options (new Array(1, 2, 3), Array(1, 2, 3), [1, 2, 3]) are correct methods to create a new array in JavaScript. The most common and concise way is using the array literal syntax, [].
  4. Which operator is used for string concatenation?
    • Explanation: The + operator is used for string concatenation in JavaScript. When used with strings, it combines them into one string. For example, "Hello" + " World" results in "Hello World".
  5. How do you declare a function in JavaScript?
    • Explanation: A function in JavaScript is declared using the function keyword followed by the function name and parentheses. For example, function myFunction() {} declares a function named myFunction.
  6. What is the output of console.log(1 + "2" + "2")?
    • Explanation: JavaScript performs operations from left to right. Here, 1 + "2" results in "12" (number 1 is concatenated with string “2”), and "12" + "2" results in "122".
  7. Which method is used to round a number to the nearest integer?
    • Explanation: The Math.round() method is used to round a number to the nearest integer. If the fractional part is 0.5 or greater, the argument is rounded to the next higher integer.
  8. How do you create an object in JavaScript?
    • Explanation: An object in JavaScript can be created using the object literal syntax {} or by using the new Object() constructor. Both var obj = {}; and var obj = new Object(); are valid.
  9. What is the purpose of Array.filter() in JavaScript?
    • Explanation: The filter() method creates a new array with all elements that pass the test implemented by the provided function. It’s used for filtering an array based on a condition.
  10. Which method removes the last element from an array and returns that element?
    • Explanation: The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
  11. How can you convert a string into an integer in JavaScript?
    • Explanation: The Number() function converts a string into an integer or a float, depending on the string format. For example, Number("123") will return the number 123.
  12. What will console.log(!!"hello") output?
    • Explanation: The !! (double negation) converts a value to a boolean. Any non-empty string is truthy in JavaScript, so !!"hello" will output true.
  13. Which statement is true about JavaScript?
    • Explanation: JavaScript is an interpreted language, not a compiled one. It’s primarily used for client-side scripting but has grown to server-side applications as well.
  14. Which of these is a correct way to define an arrow function in JavaScript?
    • Explanation: An arrow function is defined using () => {} syntax. For example, var myFunc = () => {}; is a correct definition of an arrow function.
  15. What is the purpose of try...catch statement in JavaScript?
    • Explanation: The try...catch statement is used to handle exceptions. Code in the try block is executed, and if an error occurs, it is caught in the catch block.
  16. How do you find the length of a string in JavaScript?
    • Explanation: The length of a string can be found using the .length property. For example, "Hello".length returns 5.
  17. What will console.log(3 < 2 < 1) return?
    • Explanation: This expression evaluates left-to-right. First, 3 < 2 evaluates to false, which is equivalent to 0. Then, 0 < 1 evaluates to true.
  18. Which method would you use to find an element by its ID in a document?
    • Explanation: document.getElementById("id") is used to find an HTML element by its ID.
  19. How do you write a comment in JavaScript?
    • Explanation: Comments in JavaScript are written using // for single-line comments and /* */ for multi-line comments.
  20. What is event propagation in JavaScript?
    • Explanation: Event propagation is the way some events bubble up through the DOM or are captured down. It can be stopped using event.stopPropagation().
  21. Which of these values is considered falsy in JavaScript?
    • Explanation: In JavaScript, the value 0 is considered falsy. Other falsy values include null, undefined, NaN, "" (empty string), and false.
  22. What is the output of console.log(typeof NaN)?
    • Explanation: The output is "number". In JavaScript, NaN (Not a Number) is considered a numeric value and is a type of number.
  23. How do you copy an object in JavaScript?
    • Explanation: To create a shallow copy of an object, you can use Object.assign({}, obj1) or the spread syntax {...obj1}.
  24. What does the unshift() method do in an array?
    • Explanation: The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
  25. What does typeof operator return for a function?
    • Explanation: The typeof operator returns "function" when used on a function. This is because functions are callable objects in JavaScript.

What will console.log(typeof undefined) output?

A. “undefined”

B. “object”

C. “null”

D. “string”

Which statement creates a constant in JavaScript?

A. var myConst = 10;

B. let myConst = 10;

C. const myConst = 10;

D. myConst = 10;

Which of these is a correct method to create a new array?

A. var arr = new Array(1, 2, 3);

B. var arr = Array(1, 2, 3);

C. var arr = [1, 2, 3];

D. All of the above

Which operator is used for string concatenation?

A. +

B. &

C. +=

D. ||

How do you declare a function in JavaScript?

A. function myFunction() {}

B. var myFunction() {}

C. function:myFunction() {}

D. function = myFunction() {}

What is the output of console.log(1 + “2” + “2”)?

A. “122”

B. “32”

C. “14”

D. “12” + 2

Which method is used to round a number to the nearest integer?

A. Math.round()

B. Math.floor()

C. Math.ceil()

D. Math.absolute()

How do you create an object in JavaScript?

A. var obj = {};

B. var obj = new Object();

C. var obj = new OBJECT();

D. Both A and B

What is the purpose of Array.filter() in JavaScript?

A. To modify each item in an array.

B. To create a new array based on a condition.

C. To find the first item that meets a condition.

D. To loop through an array.

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

A. pop()

B. push()

C. last()

D. get()

How can you convert a string into an integer in JavaScript?

A. int.parse(“123”)

B. Number(“123”)

C. “123”.toInt()

D. parseNumber(“123”)

What will console.log(!!”hello”) output?

A. true

B. false

C. “hello”

D. “true”

Which statement is true about JavaScript?

A. It is a compiled language.

B. It is mainly used for server-side scripting.

C. It is the same as Java.

D. It is an interpreted language.

Which of these is a correct way to define an arrow function in JavaScript?

A. var myFunc = () => {};

B. var myFunc = function() {};

C. var myFunc = >() {};

D. var myFunc => {};

What is the purpose of try…catch statement in JavaScript?

A. To handle possible errors in a block of code.

B. To test a block of code for performance.

C. To differentiate between different types of errors.

D. To declare variables that are used in a try block.

How do you find the length of a string in JavaScript?

A. str.length

B. str.len

C. str.size()

D. length(str)

What will console.log(3 < 2 < 1) return?

A. true

B. false

C. undefined

D. NaN

Which method would you use to find an element by its ID in a document?

A. document.getElement(“id”)

B. document.getElementById(“id”)

C. document.find(“id”)

D. document.querySelector(“#id”)

How do you write a comment in JavaScript?

A. /* This is a comment */

B. // This is a comment

C. # This is a comment

D. Both A and B

What is event propagation in JavaScript?

A. The bubbling of events up through the DOM.

B. The sinking of events down through the DOM.

C. The method of attaching a single event listener to manage all events.

D. Both A and C

Which of these values is considered falsy in JavaScript?

A. 0

B. 1

C. {}

D. []

What is the output of console.log(typeof NaN)?

A. “number”

B. “NaN”

C. “undefined”

D. “object”

How do you copy an object in JavaScript?

A. var obj2 = Object.assign({}, obj1);

B. var obj2 = obj1;

C. var obj2 = {…obj1};

D. Both A and C

What does the unshift() method do in an array?

A. Removes the first element.

B. Adds elements to the beginning.

C. Adds elements to the end.

D. Removes the last element.

What does typeof operator return for a function?

A. “function”

B. “object”

C. “string”

D. “undefined”

Correct Answers Table

Question NumberCorrect Answer
1A
2C
3D
4A
5A
6A
7A
8D
9B
10A
11B
12A
13D
14A
15A
16A
17A
18B
19D
20D
21A
22A
23D
24B
25A