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
- Variables, data types and operators
- Functions and scope
- Arrays and objects
- DOM and events
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?
- JavaScript is a scripting language used to make websites interactive
- JavaScript is an assembly language
- JavaScript is a compiled language
- 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?
- JavaScript is an Object-Based language
- JavaScript is an Assembly language
- JavaScript is an Object-Oriented language
- 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?
- A function enclosed with references to its inner function scope
- A function enclosed with references to its lexical environment
- A function enclosed with an object
- 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'?
- Error
- Sanfoundry_ Javascriptmcq
- Undefined
- Sanfoundry_Javascriptmcq
Answer: Sanfoundry_Javascriptmcq
The + operator concatenates strings without adding extra spaces.
5. What is the output? var js = 10; js *= 5;
- 10
- 50
- 5
- Error
Answer: 50
The *= operator multiplies the variable by the given value.
6. Arrays in JavaScript are:
- An ordered list of values
- An ordered list of objects
- An ordered list of strings
- 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 ?
- false
- true
- Compilation Error
- Runtime Error
Answer: true
The == operator compares values after type conversion.
8. What is the result of 10 === '10' ?
- false
- true
- Compilation Error
- 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));
- Exception will be thrown
- Memory leak
- Error
- Yes, perfectly
Answer: Yes, perfectly
This is an Immediately Invoked Function Expression (IIFE).
10. Which of the following is NOT a JavaScript data type?
- Null
- Undefined
- Number
- 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?
- A URL using the javascript: protocol
- A URL using the javascript: code
- A URL using the javascript: stack
- 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?
- 0
- 1
- Compiler Error
- 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?
- Position
- Window
- Location
- Standard
Answer: Window
The Window object is the global object in browsers.
14. What will the function return if the value is true?
- Compilation Error
- Runtime Error
- Yes
- 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';
- short
- 123.56
- tall
- 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?
- Function / Method
- Preprocessor
- RMI
- Compiler
Answer: Function / Method
Functions or methods execute JavaScript code.
17. What is the output of Math.abs(-7.25)?
- -7.25
- 7.25
- -7
- 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
- 10
- 5
- Error
Answer: 1
The variable b is declared as 1.
19. A JavaScript program developed on Unix will:
- Work perfectly on Windows
- Only work on Unix
- Throw errors
- 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?
- for(var i=10;i>=1;i--)
- while(i<=10)
- for(var i=1;i<=10;i++)
- do while(i==10)
Answer: for(var i=10;i>=1;i--)
This loop correctly counts backward from 10 to 1.