JavaSccript Quiz
JavaSccript Quiz
What is the correct syntax for referring to an external script called "app.js"? 6
How do you write a conditional statement for executing some statements only if "i" is equal to 5? 7
How do you create a function in JavaScript? 7
What is the output of this code? 8
Which method is used to add an element at the end of an array? 8
How can a JavaScript variable be declared conditionally? 9
What will this code return? 9
What does the forEach method do in JavaScript? 10
What is the correct syntax for referring to an external script called app.js? 10
How do you write a conditional statement for executing some statements only if i is NOT equal to 5? 11
What is the purpose of the Array.prototype.map() method in JavaScript? 11
Which operator is used to assign a value to a variable? 12
How do you declare a JavaScript variable? 13
Which function of an Array object calls a function for each element in the array? 13
How do you round the number 7.25, to the nearest integer? 14
How do you find the number with the highest value of x and y? 14
What is the correct way to write a JavaScript array? 15
Which statement creates a new object in JavaScript? 15
How do you create a function in JavaScript? 16
Which method converts JSON data to a JavaScript object? 16
What will be the output of the following code? 17
How does a for loop start? 17
What does the this keyword refer to in a JavaScript method? 18
How can you detect the client's browser name in JavaScript? 19
What will the following code return? Boolean(10 > 9) 19
Which event occurs when the user clicks on an HTML element? 20
What is the correct syntax for referring to an external script called app.js that will NOT run until after the page has
loaded? 20
What does the this keyword refer to in a JavaScript method? 21
How can you detect the client's browser name in JavaScript? 21
What will the following code return? Boolean(10 > 9) 22
Which event occurs when the user clicks on an HTML element? 22
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
1
What is the correct syntax for referring to an external script called app.js that will NOT run until after the page has
loaded? 23
What is the purpose of the Array.prototype.reduce() method in JavaScript? 24
How do you declare a JavaScript asynchronous function? 24
What is a JavaScript Promise? 25
Which method is used to serialize an object into a JSON string in JavaScript? 25
What will the following code output to the console? 26
What is the purpose of the Array.prototype.reduce() method in JavaScript? 27
How do you declare a JavaScript asynchronous function? 27
What is a JavaScript Promise? 28
Which method is used to serialize an object into a JSON string in JavaScript? 28
What will the following code output to the console? 29
What does the splice method do in an array? 30
What will the following code return? 31
Which HTML attribute is used to define inline JavaScript? 31
What is the output of the following code? 32
Which method is used to remove the last element from an array? 32
How do you declare a constant variable in JavaScript? 33
Which of the following is a correct way to create an object in JavaScript? 33
Which of these is not a valid way to loop over an array in JavaScript? 34
What will the following code output? 35
What does the && operator do? 35
How do you find the length of a string str in JavaScript? 36
What is the correct syntax for creating a new Date object in JavaScript? 36
What will the following code output to the console? 37
Which of these keywords is used to define a block of code that will execute if an exception is thrown in a try block?
37
What will be the output of the following code snippet? 38
Which of the following is true about JavaScript? 39
What is the correct syntax to output something in the console in JavaScript? 39
How do you create a function in JavaScript? 40
How do you write a conditional statement in JavaScript? 40
What is the purpose of the Array.prototype.filter() method? 41
How can you add a comment in a JavaScript file? 41
How do you declare a JavaScript variable? 42
Which of the following is a way to define an object in JavaScript? 42
What is the use of parseInt function in JavaScript? 43
What does the === operator represent in JavaScript? 44
Which method is used to decode a URI in JavaScript? 44
What will the following code output? 45
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
2
Which one is NOT a valid JavaScript data type? 45
How do you write an 'if' statement in JavaScript to check if a variable a is NOT equal to 10? 46
What does the break statement do in JavaScript? 46
Which object in JavaScript is used to work with dates? 47
What is the output of 0.1 + 0.2 === 0.3? 47
What will the following code output? 48
How can you convert the string '123' to a number in JavaScript? 48
What will be logged to the console when the following code is executed? 49
Which method should be used to compare two dates in JavaScript? 50
What is the result of the following expression: null == undefined? 50
What is event delegation in JavaScript? 51
Which of the following is the correct way to create a new empty array in JavaScript? 51
How can you stop the execution of a setInterval method? 52
What is the output of the following code? 52
What does the bind() method in JavaScript do? 53
How do you find the largest number in a list of numbers in JavaScript? 54
What does the following code return? !!(function(){})() 54
What is the output of the following code? 55
What will the following code print? 55
Which method should you use to remove an element from the beginning of an array? 56
What is the output of this code? 56
What is a typical use case for an anonymous function in JavaScript? 57
What is the result of typeof NaN? 58
How do you clone an object in JavaScript? 58
What is event bubbling in JavaScript? 59
Which of the following is a valid type of function in JavaScript? 59
What is the default behavior of the this keyword in a function? 60
What does the following code return? Boolean(" ") 60
How do you write a JavaScript array? 61
What will the following code output? 61
Which statement is used to test for a specific condition in JavaScript? 62
Which JavaScript method is used for parsing integer values? 63
What will the following code print? 63
What is the output of the following code snippet? 64
How do you create a JavaScript Promise? 64
Which of these is a correct method to create a new array in JavaScript? 65
What is the output of console.log(0.1 + 0.2 == 0.3);? 65
How do you find the character at a specific index in a string in JavaScript? 66
How do you iterate over the properties of an object in JavaScript? 67
What will be the result of console.log(typeof undefined === typeof NULL);? 67
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
3
Which method is used to add an item to the beginning of an array? 68
What does the map() method do? 68
What will console.log('B' + 'a' + + 'a' + 'a'); output? 69
What is the use of the bind() method in JavaScript? 69
What is event bubbling in JavaScript? 70
What will be the output of the following code? 70
Which of the following is NOT a valid way to declare a JavaScript variable? 71
Which method would you use to remove an item from the end of an array and return that item? 72
How do you find the number with the highest value in a list of numbers? 72
What is the purpose of Array.prototype.forEach() method in JavaScript? 73
What will be the output of the following code? 73
How do you create a JavaScript Promise object? 74
Which one of these is used to check whether a property exists in an object? 75
What will this code output? 75
Which method is used to sort the elements of an array in place and return the sorted array? 76
How do you declare a JavaScript function? 76
What will the following code output? 77
Which method would you use to convert a JSON string into a JavaScript object? 77
How do you create a new object in JavaScript? 78
What is event delegation in JavaScript? 78
Which of these is a JavaScript data type? 79
What will this code output? 80
How can you check if an object is an array in JavaScript? 80
What is the purpose of the reduce() method in an array? 81
What is the correct syntax for adding comments in JavaScript? 82
What is the correct syntax for referring to an external script called "xxx.js"? 82
How do you create a variable with the numeric value 5? 83
Which of the following is a correct way to create an array in JavaScript? 83
What keyword is used to define a function in JavaScript? 84
How do you write an IF statement in JavaScript? 84
How do you find the element with the id "demo" in a web page? 85
How do you add an event handler in JavaScript? 85
How do you create an object in JavaScript? 86
What does the async keyword do? 86
Which statement is used to handle errors in JavaScript? 86
What is the purpose of template literals in JavaScript? 87
Which loop structure is not available in JavaScript? 87
Which of the following values is considered falsy in JavaScript? 88
What is a key feature of arrow functions in JavaScript? 88
What type of scope does JavaScript use? 89
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
4
What will be the output of console.log("5" + 3); in JavaScript? 89
Which statement is true about hoisting in JavaScript? 89
What is the initial state of a JavaScript Promise? 90
What is the purpose of "use strict" in JavaScript? 90
What does event bubbling mean in the context of JavaScript events? 91
What character is used to define a template string in JavaScript? 91
How can you access the property name of an object person? 92
What does Promise.all do? 92
Which keyword is used to define a class in JavaScript? 93
What is a closure in JavaScript? 93
What does JSON stand for? 94
What is the difference between let and const in JavaScript? 94
What does destructuring mean in JavaScript? 94
What does the map method do in JavaScript? 95
What will be the output of the following code? 95
What are default parameters in JavaScript functions? 96
What is the purpose of the JavaScript event loop? 96
What is a prototype in JavaScript? 97
What does the this keyword refer to in a JavaScript function? 97
What is a higher-order function in JavaScript? 98
What is the purpose of async/await in JavaScript? 98
In JavaScript, what are modules? 98
What do spread (...) operators do in JavaScript? 99
What is garbage collection in JavaScript? 99
What does the Document.querySelector() method do in the DOM API? 100
Which of the following is an instance of an Error object in JavaScript? 100
Which method is used to store data in the local storage in a web browser? 101
Which feature is unique to template literals in JavaScript? 101
What is the main role of a JavaScript engine in a web browser? 101
What is CORS in the context of web development? 102
What are WebSockets primarily used for in JavaScript? 102
What is the main purpose of service workers in web applications? 103
What is Cross-Site Scripting (XSS)? 103
What is the purpose of the console.log function in JavaScript? 104
Which object is used for making HTTP requests in JavaScript? 104
What is the purpose of the Intl object in JavaScript? 105
How do you select all paragraph elements in a document using JavaScript? 105
Which statement about JavaScript's setTimeout() function is true? 105
Which event fires whenever a form field loses focus? 106
What does the finally block do in a try...catch statement? 106
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
5
Which data type does JavaScript use for storing decimal values? 107
What is an IIFE (Immediately Invoked Function Expression) in JavaScript? 107
What is the purpose of regular expressions in JavaScript? 107
What is the scope of a variable declared with var inside a function in JavaScript? 108
What is the Singleton pattern in JavaScript? 108
What feature of HTML5 is used to draw graphics on a web page via JavaScript? 109
What is an API in the context of JavaScript web development? 109
What does the window object represent in a browser-based JavaScript environment? 110
Which method should you use to combine two arrays in JavaScript? 110
What is the purpose of the document.createElement() method in JavaScript? 111
What is the use of the === operator in JavaScript? 111
What is a callback function in JavaScript? 111
How do you check if an object is an array in JavaScript? 112
Which JavaScript method is used to write HTML output directly to a web page? 112
What is the main benefit of using Web Workers in JavaScript? 113
How can you dynamically change the style of an HTML element using JavaScript? 113
How do you prevent a form from submitting using JavaScript? 114
What is jQuery primarily used for? 114
How do you parse a JSON string in JavaScript? 115
Why is it recommended to avoid using global variables in JavaScript? 115
What does the navigator object in JavaScript represent? 115
Which JavaScript framework is primarily used for building user interfaces? 116
How do you remove an event listener from an element in JavaScript? 116
What method of the window object is used to display an alert box in JavaScript? 117
What is a RangeError in JavaScript? 117
What is the difference between getElementById and getElementsByClassName methods in JavaScript? 118
1. <script href="app.js">
2. <script name="app.js">
3. <script src="app.js">
4. <script file="app.js">
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
6
Answer: 3. <script src="app.js">
Explanation: The correct way to refer to an external JavaScript file is using the
<script> tag with the src attribute.
1. if i = 5
2. if i == 5 then
3. if (i == 5)
4. if i = 5 then
Answer: 3. if (i == 5)
Explanation: In JavaScript, the syntax for an if statement is if (condition).
1. function = myFunction()
2. function:myFunction()
3. function myFunction()
4. create myFunction()
Answer: 3. function myFunction()
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
7
Explanation: Functions in JavaScript are defined with the function keyword,
followed by a name, followed by parentheses ().
Answer: B) "object"
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
8
Answer: A) array.push(element)
Explanation: This is a use of the ternary operator, which is a shorthand for the
if-else statement. It sets x to value1 if condition is true, and value2 if condition is
false.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
9
C) TypeError
D) NaN
Answer: B) false
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
10
B) <script name="app.js">
C) <script src="app.js">
D) <script file="app.js">
Explanation: The correct way to refer to an external JavaScript file is using the
<script src="filename"> tag.
Answer: C) if (i != 5)
Explanation: In JavaScript, != is used to check if two values are not equal. !== is
also correct, but it checks both the value and the type, which isn't specified in the
question.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
11
What is the purpose of the Array.prototype.map()
method in JavaScript?
A) To iterate over each item in an array and modify the array in place.
B) To check whether at least one element in the array passes the test
implemented by the provided function.
C) To create a new array with the results of calling a provided function on every
element in the calling array.
D) To execute a reducer function on each element of the array, resulting in a single
output value.
Answer: C) To create a new array with the results of calling a provided function on
every element in the calling array.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
12
Answer: C) =
Answer: A) forEach()
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
13
Explanation: forEach() is a method that executes a provided function once for
each array element.
Answer: B) Math.round(7.25)
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
14
Answer: C) Math.max(x, y)
Explanation: Math.max() is a function that returns the largest of the zero or more
numbers given as input parameters.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
15
Explanation: In JavaScript, a new object can be created using either the new
Object() syntax or the object literal syntax {}. Both methods are widely used and
accepted.
Answer: C) JSON.parse()
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
16
Explanation: JSON.parse() is used to parse a JSON string and construct the
JavaScript value or object described by the string.
var x = 1;
function changeX() { x = 2; }
changeX();
console.log(x);
A) 1
B) 2
C) undefined
D) Error
Answer: B) 2
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
17
B) for (i <= 5; i++)
C) for (i = 0; i <= 5; i++)
D) for (i++)
Explanation: The for loop in JavaScript is typically written with three optional
expressions: the initialization (i = 0), the condition (i <= 5), and the final expression
(i++).
Explanation: In JavaScript, this refers to the object that owns the method, i.e., the
object the method is a method of. However, the value of this can change
depending on the context in which it's used.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
18
How can you detect the client's browser name in
JavaScript?
A) navigator.appName
B) browser.name
C) client.browser
D) window.browserName
Answer: A) navigator.appName
Answer: A) true
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
19
Which event occurs when the user clicks on an HTML
element?
A) onchange
B) onclick
C) onmouseover
D) onmouseclick
Answer: B) onclick
Explanation: The onclick event occurs when the user clicks on an element. It's one
of the most commonly used events in web pages.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
20
Explanation: Using the defer attribute in the <script> tag will ensure that the script
is executed after the page has finished parsing. Note that src is used to specify the
script source, not href.
Explanation: In JavaScript, this refers to the object that owns the method, i.e., the
object the method is a method of. However, the value of this can change
depending on the context in which it's used.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
21
D) window.browserName
Answer: A) navigator.appName
Answer: A) true
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
22
C) onmouseover
D) onmouseclick
Answer: B) onclick
Explanation: The onclick event occurs when the user clicks on an element. It's one
of the most commonly used events in web pages.
Explanation: Using the defer attribute in the <script> tag will ensure that the script
is executed after the page has finished parsing. Note that src is used to specify the
script source, not href.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
23
What is the purpose of the Array.prototype.reduce()
method in JavaScript?
A) To execute a reducer function on each element of the array, resulting in a single
output value.
B) To create a new array with the results of calling a provided function on every
element in the calling array.
C) To iterate over each item in an array and modify the array in place.
D) To check whether all elements in the array pass the test implemented by the
provided function.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
24
Answer: A) async function myFunc() {}
Answer: B) An object that may produce a single value sometime in the future.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
25
D) JSON.toString()
Answer: A) JSON.stringify()
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
26
What is the purpose of the Array.prototype.reduce()
method in JavaScript?
A) To execute a reducer function on each element of the array, resulting in a single
output value.
B) To create a new array with the results of calling a provided function on every
element in the calling array.
C) To iterate over each item in an array and modify the array in place.
D) To check whether all elements in the array pass the test implemented by the
provided function.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
27
Answer: A) async function myFunc() {}
Answer: B) An object that may produce a single value sometime in the future.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
28
D) JSON.toString()
Answer: A) JSON.stringify()
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
29
What does the splice method do in an array?
A) It replaces specified elements and/or adds new elements to the array.
B) It searches the array for specified items and returns their position.
C) It joins two or more arrays, and returns a new array.
D) It removes the last element of an array, and returns that element.
Answer: A) It replaces specified elements and/or adds new elements to the array.
Answer: C) npm
Explanation: npm (Node Package Manager) is the package manager for JavaScript,
allowing users to install and manage library dependencies for their JavaScript
applications.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
30
What will the following code return?
Answer: B) "boolean"
Explanation: The expression "x" === "y" evaluates to a boolean (either true or
false). Therefore, typeof("x" === "y") returns "boolean".
Answer: A) script
Explanation: Inline JavaScript code can be written inside HTML pages within a
<script> tag.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
31
What is the output of the following code?
Answer: B) 0, 1, 2, 3
Explanation: The for...in loop in JavaScript iterates over the index or key of an array
(or object), not the elements themselves. Therefore, it logs the indices of the
array: 0, 1, 2, 3.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
32
C) shift()
D) unshift()
Answer: A) pop()
Explanation: The pop() method removes the last element from an array and
returns that element, altering the length of the array.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
33
B) var obj = new Object(name: "John", age: 30);
C) var obj = Object.create("John", 30);
D) var obj = object("John", 30);
Explanation: In JavaScript, objects are commonly created using the object literal
syntax {}, where properties and values are defined within braces.
Explanation: JavaScript provides various ways to loop over arrays, such as forEach,
for...of, and for...in. However, arr.loop is not a standard method for looping.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
34
What will the following code output?
console.log(typeof NaN);
A) "number"
B) "NaN"
C) "undefined"
D) "object"
Answer: A) "number"
Explanation: The && operator is a logical AND operator that returns true if both
operands are true, and false otherwise.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
35
How do you find the length of a string str in JavaScript?
A) str.length()
B) str.size()
C) str.length
D) length(str)
Answer: C) str.length
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
36
Explanation: The correct way to create a new Date object in JavaScript is with the
new Date() constructor.
Answer: B) "122"
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
37
C) throw
D) error
Answer: A) catch
Explanation: The catch block is used to define a block of code that will be executed
if an exception is thrown in the associated try block.
let x = "10";
let y = 20;
console.log(x + y);
A) 30
B) "1020"
C) "30"
D) 10
Answer: B) "1020"
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
38
Which of the following is true about JavaScript?
A) JavaScript is a server-side only language.
B) JavaScript can be executed both on the client side and server side.
C) JavaScript is used for styling HTML pages.
D) JavaScript can only be written inside HTML documents.
Answer: B) JavaScript can be executed both on the client side and server side.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
39
How do you create a function in JavaScript?
A) function = myFunction()
B) function:myFunction()
C) function myFunction()
D) create myFunction()
Answer: B) if (x == 5)
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
40
What is the purpose of the Array.prototype.filter()
method?
A) It creates a new array with elements that pass a test provided by a function.
B) It calls a function for each element in the array.
C) It modifies each element in an array based on a function.
D) It reduces an array to a single value.
Answer: A) It creates a new array with elements that pass a test provided by a
function.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
41
How do you declare a JavaScript variable?
A) var name;
B) v name;
C) variable name;
D) new var name;
Explanation: Variables in JavaScript are declared using the var, let, or const
keyword, followed by the variable name, e.g., var name;.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
42
Explanation: In JavaScript, objects can be defined either using object literal
notation (as in A) or by first creating an object and then adding properties to it (as
in B).
Answer: B) Array.isArray(a)
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
43
Explanation: Array.isArray(a) is the recommended way to check if a variable is an
array. It returns true if the variable is an array, otherwise false.
Explanation: The === operator represents strict equality, meaning it checks for
equality of both value and type.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
44
Explanation: Both decodeURI() and decodeURIComponent() methods are used to
decode a Uniform Resource Identifier (URI) by replacing each escape sequence in
the encoded URI with the character that it represents.
Answer: A) "undefined"
Explanation: The typeof operator returns a string indicating the type of the
unevaluated operand. For undefined, it returns the string "undefined".
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
45
Answer: D) Float
Answer: B) if (a != 10)
Explanation: if (a != 10) checks if the variable a is not equal to 10. The expression a
!== 10 also checks for strict inequality (considering the type).
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
46
Answer: D) Both A and B
Answer: A) Date
Explanation: The Date object is used in JavaScript to work with dates and times.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
47
Answer: B) false
Answer: A) "number"
Explanation: In JavaScript, the typeof operator returns the data type of its
operand. Here, typeof(42) returns "number" because 42 is a numeric literal.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
48
Answer: D) All of the above
Answer: A) 5
Explanation: The postfix increment operator (x++) increments x, but returns the
value before incrementing. So, y is assigned the value 5, and x is then incremented
to 6.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
49
Which method should be used to compare two dates in
JavaScript?
A) compare()
B) compareTo()
C) equals()
D) getTime()
Answer: D) getTime()
Explanation: To compare two dates in JavaScript, you should convert them to their
numeric value (milliseconds since the Unix epoch) using the getTime() method.
Then you can compare these numeric values.
Answer: A) true
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
50
Explanation: In JavaScript, null and undefined are loosely equal, so null ==
undefined evaluates to true.
Explanation: Event delegation is a technique where you use a single event listener
on a parent element to manage all of its child elements' events, particularly useful
for handling dynamic elements or to optimize performance.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
51
Answer: D) All of the above
Answer: A) clearInterval()
Explanation: The clearInterval() method is used to stop the executions set with the
setInterval() method.
let x = '5';
let y = x * 2;
console.log(y);
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
52
A) 10
B) "52"
C) NaN
D) TypeError
Answer: A) 10
Explanation: The bind() method in JavaScript creates a new function that, when
called, has its this keyword set to the provided value. It's often used for handling
this inside callbacks or event handlers.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
53
How do you find the largest number in a list of numbers
in JavaScript?
A) Math.max(numbers)
B) Math.max.apply(null, numbers)
C) numbers.max()
D) numbers.findMax()
Answer: A) true
Explanation: !! is used to convert the value on the right into a boolean. Since
functions are objects and objects are truthy in JavaScript, this expression returns
true.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
54
What is the output of the following code?
let num = 8;
let str = "8";
console.log(num === str);
A) true
B) false
C) TypeError
D) NaN
Answer: B) false
Explanation: The === operator checks for both value and type equality. Here, num
is a number and str is a string, so the result is false.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
55
D) 1, 2, 3, 4, 5
Answer: B) 5, 5, 5, 5, 5
Explanation: Due to JavaScript's closure and the fact that var declares a
function-scoped variable, each function created in the loop captures the same i
variable, which is 5 after the loop ends.
Answer: C) shift()
Explanation: shift() is the method used to remove the first element from an array
and return that removed element. This method changes the length of the array.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
56
console.log("1" - - "1");
A) 0
B) "11"
C) 2
D) "2"
Answer: C) 2
Answer: A) Callbacks
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
57
What is the result of typeof NaN?
A) "number"
B) "NaN"
C) "undefined"
D) "object"
Answer: A) "number"
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
58
What is event bubbling in JavaScript?
A) The event is directly handled at the captured phase.
B) An event propagates from the target element directly to the outermost
element.
C) An event propagates from the outermost element to the target element.
D) The event does not propagate in any direction.
Answer: B) An event propagates from the target element directly to the outermost
element.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
59
Explanation: JavaScript supports various function types, including anonymous
functions (functions without a name), named functions (functions with a specific
name), and immediately invoked function expressions (IIFE) or immediate
functions.
Explanation: In JavaScript, when used in a function, this refers to the global object.
In a browser, the global object is window, but the global object can be different in
other environments like Node.js.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
60
C) undefined
D) NaN
Answer: A) true
let x = 3;
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
61
let y = x++;
console.log(`x:${x}, y:${y}`);
A) x:4, y:3
B) x:3, y:4
C) x:4, y:4
D) x:3, y:3
Explanation: The post-increment operator (x++) first returns the value before
incrementing. So, y is assigned 3, and then x is incremented to 4.
Answer: B) if
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
62
Which JavaScript method is used for parsing integer
values?
A) parseInt()
B) parseValue()
C) getInteger()
D) Number.parseInt()
Answer: A) parseInt()
console.log(typeof null);
A) "null"
B) "undefined"
C) "object"
D) "number"
Answer: C) "object"
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
63
Explanation: This is a well-known quirk in JavaScript. Although null is not an
object, typeof null returns "object".
Answer: B) undefined
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
64
D) new AsyncPromise()
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
65
C) undefined
D) NaN
Answer: B) false
Answer: A) charAt(index)
Explanation: The charAt() method returns the character at the specified index in a
string.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
66
How do you iterate over the properties of an object in
JavaScript?
A) for (let key in object)
B) object.forEach(key)
C) while (let key in object)
D) for (let key of object)
Explanation: The for...in loop is used to iterate over all enumerable properties of
an object.
Answer: B) false
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
67
Explanation: typeof undefined is "undefined", but NULL is not a valid identifier in
JavaScript (JavaScript is case-sensitive and null is the valid value). This results in
typeof NULL being "undefined" === "object", which is false.
Answer: B) unshift()
Explanation: The unshift() method adds one or more elements to the beginning of
an array and returns the new length of the array.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
68
Answer: A) It performs a function on each array element and returns a new array.
Explanation: The map() method creates a new array populated with the results of
calling a provided function on every element in the calling array.
Answer: C) "BaNaN"
Explanation: This code is a tricky use of unary plus (+). The second 'a' is attempted
to be converted to a number, which results in NaN. The rest of the string
concatenation results in "BaNaN".
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
69
Answer: A) To bind a function to an object.
Explanation: The bind() method creates a new function, when invoked, has its this
keyword set to the provided value, and its arguments are any passed after the first
argument.
Answer: A) A process where an event triggers the event handlers on its parent
elements.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
70
B) "22"
C) "4"
D) SyntaxError
Answer: B) "22"
Explanation: var, let, and const are the valid ways to declare variables in
JavaScript. dim is not a JavaScript keyword for declaring variables.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
71
Which method would you use to remove an item from
the end of an array and return that item?
A) pop()
B) push()
C) shift()
D) unshift()
Answer: A) pop()
Explanation: The pop() method removes the last element from an array and
returns that element. This method changes the length of the array.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
72
Explanation: Math.max.apply(null, numbers) allows you to find the highest
number in an array of numbers. The apply() method is used because Math.max
doesn't directly accept an array as an argument.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
73
C) "object"
D) "boolean"
Answer: B) "undefined"
Explanation: The typeof operator returns a string indicating the type of the
unevaluated operand. Here, typeof undefined will return "undefined".
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
74
Which one of these is used to check whether a property
exists in an object?
A) in
B) exists
C) hasOwnProperty()
D) Both A and C
Answer: B) false
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
75
Explanation: In JavaScript, arrays are reference types. Even if two arrays have
identical elements, they are not the same when compared as they refer to
different memory locations.
Answer: B) sort()
Explanation: The sort() method sorts the elements of an array in place and returns
the sorted array. The default sort order is according to string Unicode code points.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
76
Answer: D) All of the above
Answer: B) "number"
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
77
B) JSON.stringify()
C) JSON.toObject()
D) JSON.toJavaScript()
Answer: A) JSON.parse()
Explanation: A new object in JavaScript can be created using object literal syntax
({}), the new Object() constructor, or the Object.create() method.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
78
B) Assigning a single event listener to manage all events of a particular type
C) Handling an event at the target element level
D) A way to prevent events from bubbling up or capturing down
Explanation: JavaScript has multiple data types, including number, object, and
boolean.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
79
What will this code output?
var x = 1;
function foo() {
var x = 2;
console.log(x);
}
foo();
console.log(x);
A) 1, 2
B) 2, 1
C) 2, 2
D) 1, 1
Answer: B) 2, 1
Explanation: The function foo has its own local scope and logs 2. The global
variable x remains 1 and is logged outside the function.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
80
Answer: D) Both B and C
Explanation: To check if an object is an array, you can use obj instanceof Array or
Array.isArray(obj). Both are correct, although Array.isArray() is generally preferred
for its clarity and reliability.
Answer: A) To execute a provided function once for each array element, returning
a single output value.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
81
What is the correct syntax for adding comments in
JavaScript?
A) <!-- This is a comment -->
B) /* This is a comment */
C) // This is a comment
D) Both B and C
A) <script href="xxx.js">
B) <script name="xxx.js">
C) <script src="xxx.js">
D) <script file="xxx.js">
Answer: C
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
82
Explanation: In HTML, JavaScript code is inserted between <script> tags and the
'src' attribute is used to include an external script file.
A) var num = 5;
B) num = 5;
C) var 5 = num;
D) num5 = var;
Answer: A
Explanation: The correct way to declare a variable in JavaScript is using the var, let,
or const keyword, followed by the variable name and its value.
Data Types and Structures:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
83
Functions and Scope:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
84
How do you find the element with the id "demo" in a
web page?
A) document.getElement("demo");
B) document.getElementById("demo");
C) document.getElementsById("demo");
D) document.querySelector("#demo");
Answer: B
Explanation: document.getElementById("id") is the correct method to find an
element by its ID.
Events:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
85
How do you create an object in JavaScript?
A) var obj = new Object();
B) var obj = {};
C) Both A and B
D) None of the above
Answer: C
Explanation: Both new Object() and {} are valid ways to create an object in
JavaScript.
Asynchronous Programming:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
86
B) error()
C) try...catch
D) if...else
Answer: C
Explanation: The try...catch statement is used to catch errors in a block of code.
ES6 Features:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
87
Which of the following values is considered falsy in
JavaScript?
A) "0"
B) []
C) null
D) {}
Answer: C
Explanation: In JavaScript, falsy values include 0, "" (empty string), null, undefined,
NaN, and false. Arrays [] and objects {} are considered truthy.
Arrow Functions:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
88
What type of scope does JavaScript use?
A) Lexical scope
B) Dynamic scope
C) Global scope
D) Local scope
Answer: A
Explanation: JavaScript uses lexical scoping, where the scope of a variable is
defined by its location within the source code.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
89
C) Functions are not hoisted
D) Hoisting applies to variables declared with let and const
Answer: A
Explanation: In JavaScript, variable declarations are hoisted to the top of their
scope, but their initializations are not. Function declarations are also hoisted, but
not the function expressions or arrow functions.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
90
Explanation: "use strict" enforces stricter parsing and error handling in JavaScript,
helping to avoid common coding mistakes and unsafe actions.
Event Bubbling and Capturing:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
91
D) Forward slashes /
Answer: C
Explanation: Template strings in JavaScript are enclosed by backticks `. They can
contain placeholders which are indicated by ${expression} syntax.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
92
Explanation: Promise.all waits for all promises to resolve or for any to reject. If all
resolve, it resolves with an array of the results. If any reject, it rejects with the
reason of the first promise that rejected.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
93
What does JSON stand for?
A) JavaScript Object Notation
B) Java Standard Output Network
C) JavaScript Oriented Notation
D) Just a Simple Object Notation
Answer: A
Explanation: JSON stands for JavaScript Object Notation. It's a lightweight format
for storing and transporting data.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
94
B) An error that occurs when a script is not properly compiled
C) The process of assigning the properties of an object or array to variables using a
syntax that looks similar to array or object literals
D) Removing an element from an array or object
Answer: C
Explanation: Destructuring in JavaScript is a syntax for extracting multiple
properties from an array or object and assigning them to variables.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
95
C) ReferenceError: x is not defined
D) null
Answer: A
Explanation: Due to variable hoisting in JavaScript, x is declared before it is used,
but its initialization is not hoisted. Therefore, the value of x is undefined when
console.log is called.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
96
Explanation: The JavaScript event loop is a mechanism that handles asynchronous
callbacks. It's responsible for executing the code, collecting and processing events,
and executing queued sub-tasks.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
97
What is a higher-order function in JavaScript?
A) A function that operates on other functions, either by taking them as
arguments or by returning them
B) A function that is executed at a higher priority level
C) A function that returns higher values than other functions
D) A function that can only be executed after the page has loaded
Answer: A
Explanation: Higher-order functions are functions that take other functions as
arguments or return them as results.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
98
D) Built-in methods for manipulating strings and arrays
Answer: B
Explanation: JavaScript modules are a way to split a program into separate files,
allowing for better maintainability and reusability of code.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
99
What does the Document.querySelector() method do in
the DOM API?
A) Adds a new query to the document
B) Returns the first element within the document that matches the specified
group of selectors
C) Queries the server for updated content
D) Modifies the CSS of elements in the document
Answer: B
Explanation: Document.querySelector() returns the first Element within the
document that matches the specified selector or group of selectors. If no matches
are found, null is returned.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
100
Which method is used to store data in the local storage in
a web browser?
A) localStorage.store()
B) localStorage.save()
C) localStorage.setItem()
D) localStorage.put()
Answer: C
Explanation: The localStorage.setItem() method is used to store data in the
browser's local storage, allowing data to be stored across browser sessions.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
101
C) To parse and execute JavaScript code
D) To manage browser storage systems
Answer: C
Explanation: The JavaScript engine in a web browser is responsible for parsing and
executing JavaScript code, which enables dynamic content and interactive
features on web pages.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
102
Explanation: WebSockets provide a way to establish a persistent, real-time,
bi-directional communication channel between a client and a server, enabling
real-time data transfer.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
103
Explanation: Cross-Site Scripting (XSS) is a security vulnerability that allows
attackers to inject malicious scripts into webpages, which can then be executed in
the context of other users' browsers.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
104
What is the purpose of the Intl object in JavaScript?
A) To provide language sensitive string comparison, number formatting, and date
and time formatting
B) To handle international domain names and URLs
C) To automatically translate web pages into different languages
D) To format JavaScript code according to international coding standards
Answer: A
Explanation: The Intl object is a namespace for the JavaScript Internationalization
API, which provides language-sensitive string comparison, number formatting, and
date and time formatting.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
105
B) It executes a function after waiting a specified number of milliseconds.
C) It is used to set the time for a JavaScript date object.
D) It calls a function continuously at specified time intervals.
Answer: B
Explanation: setTimeout() is used to execute a function once after a specified
duration in milliseconds.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
106
Which data type does JavaScript use for storing decimal
values?
A) float
B) decimal
C) number
D) double
Answer: C
Explanation: JavaScript uses the number data type for all numbers, including
integers and decimals.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
107
D) To encode and decode JSON data
Answer: C
Explanation: Regular expressions are used in JavaScript for searching, editing, and
manipulating text strings.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
108
Explanation: The Singleton pattern restricts a class to a single instance and
provides a global point of access to that instance.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
109
Explanation: In web development, an API (Application Programming Interface) is
an interface that allows your application to interact with external services or
systems, facilitating data exchange and integration.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
110
What is the purpose of the document.createElement()
method in JavaScript?
A) To delete an existing HTML element
B) To update the content of an HTML element
C) To create a new HTML element
D) To find an existing HTML element
Answer: C
Explanation: The document.createElement() method creates a new HTML element
of the specified type.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
111
C) A function that counts the number of times a function is called
D) A function that is automatically called whenever an error occurs
Answer: B
Explanation: In JavaScript, a callback function is a function passed into another
function as an argument, which is then executed at a specified point within the
outer function.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
112
Explanation: document.write() is used to write directly to the HTML document.
However, it is generally not recommended for use in production code due to its
invasive nature.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
113
Explanation: In JavaScript, you can dynamically change the style of an HTML
element using element.style.property = 'value', where property is the CSS
property you want to change and value is the new style value.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
114
How do you parse a JSON string in JavaScript?
A) JSON.parse(jsonString)
B) JSON.stringify(jsonString)
C) JSON.decode(jsonString)
D) JSON.toObject(jsonString)
Answer: A
Explanation: JSON.parse(jsonString) is used to convert a JSON string into a
JavaScript object.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
115
C) The operating system
D) The user's browser and its capabilities
Answer: D
Explanation: The navigator object contains information about the user's browser,
such as the name, version, and platform.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
116
Explanation: element.removeEventListener(eventName, handlerFunction) is used
to remove an event listener that was added with addEventListener.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
117
What is the difference between getElementById and
getElementsByClassName methods in JavaScript?
A) getElementById returns a single element, while getElementsByClassName
returns a collection of elements
B) getElementById can select multiple elements, while getElementsByClassName
can only select one
C) getElementById is faster and more efficient than getElementsByClassName
D) getElementById applies to HTML elements only, while
getElementsByClassName applies to both HTML and XML elements
Answer: A
Explanation: getElementById retrieves a single element with the specified ID,
while getElementsByClassName returns a live HTMLCollection of all elements with
the specified class name.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
118