JavaScript Quiz

JavaScript is the programming language of the web. This quiz tests variables, data types, functions, operators, arrays, objects and how code interacts with a page, so you can find weak spots before an interview or exam.

What this quiz covers

Study tip: Run small snippets in the browser console after each question; seeing the real output makes type coercion and scope rules stick.

Questions, answers and explanations

1. What is JavaScript?

  1. JavaScript is a scripting language used to make websites interactive
  2. JavaScript is an assembly language
  3. JavaScript is a compiled language
  4. None of the mentioned

Answer: JavaScript is a scripting language used to make websites interactive

JavaScript is a scripting language that adds interactivity to web pages.

2. Which of the following is correct about JavaScript?

  1. JavaScript is an Object-Based language
  2. JavaScript is an Assembly language
  3. JavaScript is an Object-Oriented language
  4. JavaScript is a High-level language

Answer: JavaScript is a High-level language

JavaScript is a high-level, interpreted programming language.

3. What defines a closure in JavaScript?

  1. A function enclosed with references to its inner function scope
  2. A function enclosed with references to its lexical environment
  3. A function enclosed with an object
  4. None of the mentioned

Answer: A function enclosed with references to its lexical environment

A closure allows a function to access variables from its lexical scope even after the outer function has returned.

4. What is the output of txt1 + txt2 if txt1='Sanfoundry_' and txt2='Javascriptmcq'?

  1. Error
  2. Sanfoundry_ Javascriptmcq
  3. Undefined
  4. Sanfoundry_Javascriptmcq

Answer: Sanfoundry_Javascriptmcq

The + operator concatenates strings without adding extra spaces.

5. What is the output? var js = 10; js *= 5;

  1. 10
  2. 50
  3. 5
  4. Error

Answer: 50

The *= operator multiplies the variable by the given value.

6. Arrays in JavaScript are:

  1. An ordered list of values
  2. An ordered list of objects
  3. An ordered list of strings
  4. An ordered list of functions

Answer: An ordered list of values

JavaScript arrays can store multiple values of different data types.

7. What is the result of 2 == 2.0 ?

  1. false
  2. true
  3. Compilation Error
  4. Runtime Error

Answer: true

The == operator compares values after type conversion.

8. What is the result of 10 === '10' ?

  1. false
  2. true
  3. Compilation Error
  4. Runtime Error

Answer: false

The === operator compares both value and data type.

9. Will this code work? var js = (function(x){ return x*x; }(10));

  1. Exception will be thrown
  2. Memory leak
  3. Error
  4. Yes, perfectly

Answer: Yes, perfectly

This is an Immediately Invoked Function Expression (IIFE).

10. Which of the following is NOT a JavaScript data type?

  1. Null
  2. Undefined
  3. Number
  4. All of the mentioned

Answer: All of the mentioned

Null, Undefined, and Number are all valid JavaScript data types.

11. Where is client-side JavaScript embedded in HTML?

  1. A URL using the javascript: protocol
  2. A URL using the javascript: code
  3. A URL using the javascript: stack
  4. A URL using the javascript: encoding

Answer: A URL using the javascript: protocol

The javascript: protocol allows JavaScript code to run from a URL.

12. What is the result if a variable is not null?

  1. 0
  2. 1
  3. Compiler Error
  4. Runtime Error

Answer: 1

If the variable is not null, the condition is true.

13. Which object is the main entry point to all client-side JavaScript features?

  1. Position
  2. Window
  3. Location
  4. Standard

Answer: Window

The Window object is the global object in browsers.

14. What will the function return if the value is true?

  1. Compilation Error
  2. Runtime Error
  3. Yes
  4. No

Answer: Yes

The ternary operator returns 'Yes' when the condition is true.

15. What is the output? var height = 123.56; height >= 190 ? 'tall' : 'short';

  1. short
  2. 123.56
  3. tall
  4. 190

Answer: short

123.56 is less than 190, so the result is 'short'.

16. Which of the following is used to call a JavaScript code snippet?

  1. Function / Method
  2. Preprocessor
  3. RMI
  4. Compiler

Answer: Function / Method

Functions or methods execute JavaScript code.

17. What is the output of Math.abs(-7.25)?

  1. -7.25
  2. 7.25
  3. -7
  4. 7

Answer: 7.25

Math.abs() returns the absolute (positive) value.

18. What is the value of 'b' in the following code? var b = 1;

  1. 1
  2. 10
  3. 5
  4. Error

Answer: 1

The variable b is declared as 1.

19. A JavaScript program developed on Unix will:

  1. Work perfectly on Windows
  2. Only work on Unix
  3. Throw errors
  4. Display JavaScript as text

Answer: Work perfectly on Windows

JavaScript is platform-independent and works in browsers on different operating systems.

20. Which loop is correct for counting from 10 down to 1?

  1. for(var i=10;i>=1;i--)
  2. while(i<=10)
  3. for(var i=1;i<=10;i++)
  4. do while(i==10)

Answer: for(var i=10;i>=1;i--)

This loop correctly counts backward from 10 to 1.

Take the timed quiz