JavaScript QUIZ 25 Test your Knowledge!
- 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 withundefined
, it returns the string"undefined"
. This is becauseundefined
is a primitive type in JavaScript representing the absence of a defined value.
- Explanation: The
- 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 namedmyConst
with the value10
.
- Explanation: In JavaScript, the
- 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,[]
.
- Explanation: All given options (
- 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"
.
- Explanation: The
- 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 namedmyFunction
.
- Explanation: A function in JavaScript is declared using the
- 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"
.
- Explanation: JavaScript performs operations from left to right. Here,
- 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.
- Explanation: The
- How do you create an object in JavaScript?
- Explanation: An object in JavaScript can be created using the object literal syntax
{}
or by using thenew Object()
constructor. Bothvar obj = {};
andvar obj = new Object();
are valid.
- Explanation: An object in JavaScript can be created using the object literal syntax
- 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.
- Explanation: The
- 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.
- Explanation: The
- 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.
- Explanation: The
- 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 outputtrue
.
- Explanation: The
- 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.
- 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.
- Explanation: An arrow function is defined using
- What is the purpose of
try...catch
statement in JavaScript?- Explanation: The
try...catch
statement is used to handle exceptions. Code in thetry
block is executed, and if an error occurs, it is caught in thecatch
block.
- Explanation: The
- 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.
- Explanation: The length of a string can be found using the
- What will
console.log(3 < 2 < 1)
return?- Explanation: This expression evaluates left-to-right. First,
3 < 2
evaluates tofalse
, which is equivalent to0
. Then,0 < 1
evaluates totrue
.
- Explanation: This expression evaluates left-to-right. First,
- 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.
- Explanation:
- How do you write a comment in JavaScript?
- Explanation: Comments in JavaScript are written using
//
for single-line comments and/* */
for multi-line comments.
- Explanation: Comments in JavaScript are written using
- 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()
.
- Explanation: Event propagation is the way some events bubble up through the DOM or are captured down. It can be stopped using
- Which of these values is considered falsy in JavaScript?
- Explanation: In JavaScript, the value
0
is considered falsy. Other falsy values includenull
,undefined
,NaN
,""
(empty string), andfalse
.
- Explanation: In JavaScript, the value
- 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.
- Explanation: The output is
- 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}
.
- Explanation: To create a shallow copy of an object, you can use
- 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.
- Explanation: The
- 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.
- Explanation: The
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 Number | Correct Answer |
1 | A |
2 | C |
3 | D |
4 | A |
5 | A |
6 | A |
7 | A |
8 | D |
9 | B |
10 | A |
11 | B |
12 | A |
13 | D |
14 | A |
15 | A |
16 | A |
17 | A |
18 | B |
19 | D |
20 | D |
21 | A |
22 | A |
23 | D |
24 | B |
25 | A |