JavaScript Quiz Questions
JavaScript Quiz Questions
Answer: d) Object
Explanation: In JavaScript, the primitive data types are Number,
String, Boolean, Null, and Undefined. Object is not a primitive
data type.
Answer: b) "22"
Explanation: When a number is concatenated with a string,
JavaScript will convert the number to a string and concatenate
the two values. Therefore, the output of the code is "22".
d) =<
Answer: d) =<
Explanation: The correct comparison operator is "<=", not "=<".
Answer: b) 5
a) true
b) false
c) undefined
d) Error
Answer: b) false
a) true
b) false
c) undefined
d) Error
Answer: a) true
Explanation: The "==" operator in JavaScript performs type
coercion, which means that the values are converted to a
common type before being compared. In this case, the string "5"
is converted to the number 5, so the values of x and y are equal.
Answer: d) let x = 5;
Explanation: In modern JavaScript, the recommended way to
declare a variable is using the "let" keyword.
console.log(x.length);
a) 1
b) 2
c) 3
d) 4
Answer: c) 3
Explanation: The "length" property in JavaScript returns the
number of elements in an array. In this case, the array "x" has
three elements, so the output is 3.
Answer: a) "number"
Explanation: Although NaN stands for "Not a Number", its type in
JavaScript is actually "number".
Answer: c) 8
Explanation: The "**" operator in JavaScript is the exponentiation
operator, which raises the first operand to the power of the
second operand. In this case, 2 raised to the power of 3 is 8.
Answer: b) false
Explanation: The "===" operator in JavaScript performs a strict
comparison, which means that the values and the types must be
equal for the comparison to return true. In this case, x is a
number and y is a string
Answer: a) 5
Explanation: When JavaScript tries to perform arithmetic
operations with a string and a number, it automatically converts
the string to a number before making the calculation. In this
case, "5" is converted to 5, so the result of the subtraction is 5.
Answer: a) [1, 2, 3]
Answer: d) Character
Explanation: In JavaScript, there is no data type called
"Character". Instead, characters are represented using the
"String" data type.
Answer: c) "55"
Explanation: When you use the "+" operator with a string and a
number, JavaScript converts the number to a string and
concatenates it to the string.
Answer: b) 5
Explanation: In JavaScript, "var" variables are
function-scoped, not block-scoped. This means that the
"x" variable inside the if statement is the same as the
"x" variable outside of it, and changing its value
inside the block also changes its value outside the
block.
Answer: b) undefined
Explanation: In JavaScript, arrays are zero-indexed, which means
that the first element of an array has an index of 0. Since the "x"
array has only three elements, attempting to access the fourth
element using an index of 3 results in undefined.
Answer: d) function = {}
Explanation: The syntax for declaring a function in JavaScript is
either "function functionName() {}" or "var functionName =
function() {}", or using an arrow function expression "() => {}".
The option d) is not a valid way to declare a function in
JavaScript.
d) "string"
Answer: a) "undefined"
Explanation: "undefined" is a primitive data type in JavaScript
that represents the absence of a value. The typeof operator
returns a string indicating the type of the operand.
Answer: a) "123"
Explanation: When you use the "+" operator with a string and a
number, JavaScript converts the number to a string and
concatenates it to the string. So in this case, "1" is concatenated
with "2" to create "12", and then "3" is concatenated to create
"123".
Answer: d) next
Answer: d) slice()
Explanation: "slice()" is a valid array method in JavaScript, but it
is not a method for modifying the array. Instead, it returns a
shallow copy of a portion of the array as a new array.
Answer: b) "object"
Explanation: In JavaScript, arrays are a type of object. The typeof
operator returns "object" for any object type, including arrays.
var x = 5;
}
foo();
a) 10
b) 5
c) undefined
d) Error
Answer: c) undefined
Explanation: In JavaScript, variable declarations are hoisted to
the top of the function, but their assignments are not. So the "x"
variable inside the "foo" function is hoisted, but it is not assigned
a value until after the console.log statement. Therefore, the
output is "undefined".
Answer: b) 2
Explanation: In JavaScript, inner functions have access to the
variables of their outer functions, even after the outer function
has returned. In this case, the "baz" variable is assigned the "bar"
function returned by "foo", and when "baz" is called, it accesses
the "x" variable of its parent function "foo".
Answer: a) true
Explanation: In JavaScript, the "==" operator performs type
coercion before comparison. In this case, the string "5" is
converted to the number 5 before comparison with the number 5,
resulting in true.
Answer: d) variable w;
Answer: b) "object"
Explanation: In JavaScript, the typeof operator returns "object"
for the null value. This is considered to be a historical bug in the
language.
Answer: b) "e"
Explanation: In JavaScript, the "charAt" method is used to access
a character in a string by its index. In this case, the character at
index 1 is "e".
Answer: a) 5
Explanation: In JavaScript, inner functions have access to the
variables of their outer functions, even if the outer function has
already returned. In this case, the "foo" function accesses the "x"
variable in the global scope, which has a value of 5. The "x"
variable declared in the "bar" function is a separate variable with
a different scope.
Answer: a) "54"
Explanation: In JavaScript, the "+" operator performs both
addition and string concatenation. When the "+" operator is used
with both numbers and strings, it performs addition first and then
concatenates the result with the remaining string. In this case, 2
+ 3 = 5, and then "4" is concatenated to create "54".
Answer: b) false
Answer: b) 5
Explanation: In JavaScript, the "++" operator is used to
increment a variable by 1. When used after the variable (i.e.
x++), the increment happens after the current value is used. In
this case, the value of x is 5, so y is assigned the value of 5
before x is incremented to 6.
Answer: b) [2, 3]
Explanation: In JavaScript, the "slice" method is used to create a
new array that contains a portion of an existing array. In this
case, the "y" variable is assigned a new array that contains all
elements of "x" starting from index 1 (i.e. [2, 3]).
Answer: c) 3
Explanation: In JavaScript, the "length" property of an array
returns the number of elements in the array. In this case, the "x"
array has 3 elements, so the output is 3.
Answer: a) "h"
Answer: a) "number"
Explanation: In JavaScript, the "typeof" operator is used to get
the type of a value or variable. In this case, "x" is assigned the
value of 5, which is a number, so the output is "number".
Answer: b) "52"
Answer: c) [1, 2, 3, 4]
Explanation: In JavaScript, the "push" method is used to add an
element to the end of an array. In this case, the "x" array already
contains the elements [1, 2, 3], and the "push(4)" statement
adds the element 4 to the end of the array, resulting in the new
array [1, 2, 3, 4].
Answer: c) 15
Explanation: In JavaScript, when the "-" operator is used with a
number and a string, the string is converted to a number and the
subtraction is performed. In this case, "x" is the number 10, and
"y" is the string "5". However, because the "-" operator can also
be used for string concatenation, the string "5" is first converted
to the number 5, and then subtracted from 10 to get 15.
Answer: a) 1
Explanation: In JavaScript, arrays are indexed starting from 0.
The square brackets notation is used to access an element of an
array by its index. In this case, "x" is the array [1, 2, 3], and
"x[0]" accesses the first element of the array, which is the
number 1.
Answer: c) "510"
Explanation: In JavaScript, the "+" operator performs both
addition and string concatenation. When the "+" operator is used
with a number and a string, the number is converted to a string
and then concatenated with the original string. In this case, "x" is
the number 5, and "y" is the string "10", so "x + y" concatenates
the two values to create the string "510".
Answer: b) [1, 2, 3, 4]
Explanation: In JavaScript, the "push" method is used to add an
element to the end of an array. In this case, "x" is the array [1,
2, 3], and "x.push(4)" adds the number 4 to the end of the array
to create the array [1, 2, 3, 4].
Answer: c) "105"
Answer: a) number
Explanation: In JavaScript, the "typeof" operator is used to
determine the data type of a value. In this case, "x" is the
number 10, so "typeof x" returns the string "number".
Answer: d) 3
Answer: a) 5
Explanation: In JavaScript, when the "-" operator is used with
two strings, both strings are converted to numbers and the
subtraction is performed. In this case, "x" is the string "10", and
"y" is the string "5". Both strings are converted to numbers (10
and 5, respectively), and then 5 is subtracted from 10 to get 5.
Answer: b) 6
Answer: b) 2
Explanation: In JavaScript, arrays are zero-indexed, meaning the
first element in the array has an index of 0, the second element
has an index of 1, and so on. In this case, "x" is the array [1, 2,
3], and "x[1]" accesses the second element of the array (which
has an index of 1), which is the number 2.
b) Not equal
c) Undefined
d) Error
Answer: a) Equal
Explanation: In JavaScript, the "==" operator performs a loose
equality comparison, which means it compares the values without
considering their data types. In this case, "x" is the number 10,
and "x == '10'" returns true because the string "10" is converted
to the number 10 before the comparison is made. So, the "if"
statement evaluates to true, and "Equal" is printed to the
console.
Answer: b) "HELLO"
Explanation: In JavaScript, the "toUpperCase" method is used to
convert a string to all uppercase letters. In this case, "x" is the
string "hello", and "x.toUpperCase()" converts the string to
"HELLO" and prints it to the console.
var y = false;
console.log(x || y);
a) true
b) false
c) null
d) Error
Answer: a) true
Explanation: In JavaScript, the "||" operator performs a logical
OR operation. The expression "x || y" evaluates to true if either
"x" or "y" is true. In this case, "x" is true, so the expression "x ||
y" evaluates to true and is printed to the console.
Answer: a) "string"
Explanation: In JavaScript, the "typeof" operator is
used to determine the data type of a variable. In this
case, "x" is a string because it is enclosed in
quotation marks.
var y = "4";
console.log(x + y);
a) "34"
b) "7"
c) 7
d) Error
Answer: a) "34"
Explanation: In JavaScript, the "+" operator is used to
concatenate strings and add numbers. If one or both
operands are strings, the "+" operator performs string
concatenation. In this case, "x" is the number 3 and
"y" is the string "4", so the expression "x + y"
converts the number 3 to a string and concatenates it
with the string "4", resulting in the string "34".
Answer: b) [1, 2, 3, 4]
Explanation: In JavaScript, the "push" method is used to add
elements to the end of an array. In this case, "x" is the array [1,
2, 3], and "x.push(4)" adds the number 4 to the end of the array,
resulting in the array [1, 2, 3, 4].
Answer: b) 10 9 8 7 6 5 4 3 2 1
Explanation: In JavaScript, the "while" loop is used to repeatedly
execute a block of code while a condition is true. In this case, the
loop will execute as long as "x" is greater than 0. In each
iteration of the loop, the value of "x" is printed to the console
using the "console.log" function, and then "x" is decremented by
1.
Answer: b) [1, 2, 3, 4]
Explanation: In JavaScript, arrays and objects are reference
types, which means that when a variable is assigned to an array
or object, it holds a reference to the location in memory where
the array or object is stored. In this case, "y" is assigned to the
same array as "x" using the assignment operator, so both
variables reference the same array. When "y.push(4)" is called, it
modifies the array that both "x" and "y" reference, so the output
of "console.log(x)" is the modified array [1, 2, 3, 4].
Answer: b) 10
Explanation: In JavaScript, the "++" operator is used to
increment a variable by 1. When "x++" is called, the value of "x"
is first used as the operand of the expression, and then it is
incremented by 1. The value of the expression "x++" is the
original value of "x" before it was incremented, so "y" is assigned
the value 10. The output of "console.log(y)" is therefore 10.
Answer: c) 15
Explanation: In JavaScript, the "*=" operator is used to perform
multiplication and assignment in a single step. The expression "x
*= y" is equivalent to "x = x * y", which multiplies the value of
"x" by the value of "y" and assigns the result back to "x". The
output of "console.log(x *= y)" is therefore the product of 5 and
3, which is 15.
Answer: b) "13"
Explanation: In JavaScript, the unary "+" operator can be used to
convert a string to a number. When the expression "+ '3'" is
evaluated, the string "3" is converted to the number 3. The
expression "x + y" then concatenates the string "10" with the
string "3", resulting in the string "13". The output of
"console.log(x + y)" is therefore "13".
c) 2 4 6 8
d) Error
Answer: b) 0 2 4
Answer: c) "52"
Explanation: In JavaScript, the "+" operator is used for both
addition and string concatenation. When one or both operands
are strings, the "+" operator concatenates the strings. In this
case, the string "5" is concatenated with the number 2, resulting
in the string "52". The output of "console.log("5" + 2)" is
therefore "52".
Answer: b) [2, 3]
Answer: d) undefined 5
Explanation: In JavaScript, variable declarations are "hoisted" to
the top of their scope, which means that they are processed
before any other code in the same scope. However, variable
assignments are not hoisted. In this case, the "foo" function
includes a variable declaration "var x = 5" after the first
"console.log" statement. This means that the "x" variable used in
the first "console.log" statement refers to the local variable
declared in the "foo" function, not the global variable with the
same name. However, at the time of the first "console.log"
statement, the local variable "x" has not yet been assigned a
value, so its value is undefined.
a) "null"
b) "object"
c) "undefined"
d) Error
Answer: b) "object"
Explanation: In JavaScript, the "typeof" operator is used to
determine the data type of a value or variable. When used with
the "null" keyword, "typeof" returns "object". This is a historical
artifact of JavaScript, as "null" was originally intended to
represent an empty object reference.
Answer: b) [1, 2, 3, 4]
Explanation: In JavaScript, arrays are objects, and
objects are passed by reference rather than by value.
This means that when "y" is assigned the value of "x",
it is actually assigned a reference to the same array
in memory. Therefore, when "y.push(4)" is called, it
modifies the same array that "x" refers to. The output
of "console.log(x)" is therefore [1, 2, 3, 4].
Answer: a) true
Explanation: In JavaScript, the "==" operator is used to compare
two values for equality. When the values being compared have
different data types, JavaScript will attempt to convert one or
both of the values to a common data type before making the
comparison. In this case, the string "5" is converted to the
number 5 before the comparison is made. Therefore, the output
of "console.log(5 == "5")" is true.
Answer: a) 10
Answer: a) 2
Explanation: In JavaScript, the "++" and "--" operators are used
to increment and decrement a variable's value by 1, respectively.
The placement of these operators determines whether the
variable's value is incremented or decremented before or after an
expression is evaluated. In this case, the expression "x++ - y--"
is evaluated as follows:
Answer: a) "hello3"
Explanation: In JavaScript, the "+" operator is used for both
addition and concatenation. When used with a string and a
number, it performs concatenation rather than addition.
Therefore, the output of "console.log("hello" + 3)" is "hello3".
Answer: b) "hello"
Explanation: In JavaScript, the "&&" operator is used for logical
AND. It returns the first falsy value it encounters, or the last
truthy value if all values are truthy. In this case, both "true" and
"hello" are truthy values, so the expression evaluates to "hello".
function foo() {
console.log(x);
var x = 10;
}
foo();
a) 5
b) 10
c) undefined
d) Error
Answer: a) 5
Explanation: In JavaScript, variable declarations are processed
before any other code in the same scope, but their values are not
assigned until they are reached in the code. Therefore, the "var x
= 10;" statement in the "foo" function does not affect the value
of "x" in the outer scope. When "foo" is called, it outputs the
value of "x" in the outer scope, which is 5. The output of
"console.log(x)" is therefore 5.
Answer: a) "number"
Explanation: In JavaScript, NaN (Not a Number) is a special value
of the number data type that represents an undefined or
unrepresentable value resulting from a mathematical operation.
Answer: a) [1, 2]
Explanation: In JavaScript, the "slice" method is used to extract a
portion of an array into a new array. It takes two arguments: the
starting index and the ending index (exclusive) of the slice. In
this case, "x.slice(0, 2)" extracts the elements at indices 0 and 1
(i.e. [1, 2]) into a new array, which is assigned to "y". Therefore,
the output of "console.log(y)" is [1, 2].
Answer: a) "32"
Answer: a) true
Explanation: In JavaScript, null and undefined are equal to each
other and to no other value. Therefore, the output of
"console.log(null == undefined)" is true.
Answer: a) 5
Answer: b) false
Explanation: In JavaScript, floating-point numbers are
represented using binary fractions, which can sometimes result in
rounding errors. Therefore, the expression "0.1 + 0.2" does not
equal exactly 0.3, but instead equals a slightly larger number
(e.g. 0.30000000000000004). Therefore, the output of
"console.log(0.1 + 0.2 == 0.3)" is false.
Answer: a) "number"
Explanation: In JavaScript, NaN (Not-a-Number) is a special
value of the number type. Therefore, the output of
"console.log(typeof NaN)" is "number".
Answer: b) 1
Explanation: In JavaScript, the "indexOf" method is used to find
the index of the first occurrence of a substring within a string. In
Answer: b) 6, 5
Explanation: In this code, the variable "x" is assigned the value 5
and then incremented using the "++" operator. The variable "y"
is assigned the original value of "x" (i.e. 5) before the
incrementation. Therefore, the output of "console.log(x, y)" is 6,
5.
Answer: a) true
Explanation: In JavaScript, the "<" operator performs a numerical
comparison between two values. Therefore, the expression "10 <
9 < 8" is evaluated as "(10 < 9) < 8", which first evaluates "10 <
9" to false and then "false < 8" to true. Therefore, the output of
"console.log(10 < 9 < 8)" is true.
Answer: b) [2, 4, 6]
Explanation: In JavaScript, the "map" method is used to create a
new array by applying a function to each element of an existing
array. In this case, the "map" method is used to create a new
array "y" where each element is twice the value of the
corresponding element in the original array "x". Therefore, the
output of "console.log(y)" is [2, 4, 6].
Answer: a) "object"
Explanation: In JavaScript, the "typeof" operator returns the data
type of a value. However, there is a known bug where "typeof
null" returns "object" instead of "null". Therefore, the output of
"console.log(typeof null)" is "object".
Answer: a) true
Explanation: In JavaScript, the "==" operator performs type
coercion before comparison. In this case, the number 5 and the
string "5" are coerced to the same type (i.e. number) before
comparison. Therefore, the output of "console.log(x == y)" is
true.
Answer: b) false
Explanation: In JavaScript, the "===" operator performs
strict comparison without type coercion. In this case,
the number 5 and the string "5" are of different types,
so strict comparison will always return false.
Therefore, the output of "console.log(x === y)" is
false.
b) [1, 2, 3]
c) ["1", "2", "3"]
d) Error
Answer: a) "1,2,3"
Explanation: In JavaScript, the "toString" method is used to
convert an array to a string by concatenating its elements with
commas. Therefore, the output of "console.log(x.toString())" is
"1,2,3".
Answer: b) 2
Explanation: In JavaScript, the %= operator is the modulus
assignment operator, which computes the modulus of two
operands and assigns the result to the left operand. Therefore, x
%= y is equivalent to x = x % y, which evaluates to 2. The
output of console.log(x %= y) is 2.
b) "object"
c) "null"
d) "undefined"
Answer: b) "object"
Explanation: In JavaScript, an array is a special type of object
that can hold a collection of values. Therefore, the typeof
operator returns "object" when applied to an array.
Answer: b) [1, 2, 3, 4]
Explanation: In JavaScript, the push() method adds one or more
elements to the end of an array and returns the new length of the
array. Therefore, after x.push(4), the array x contains the
elements [1, 2, 3, 4]. The output of console.log(x) is [1, 2, 3, 4].
b) "HELLO"
c) "Hello"
d) Error
Answer: b) "HELLO"
Explanation: In JavaScript, the toUpperCase() method is used to
convert a string to uppercase letters. Therefore, after y =
x.toUpperCase(), the value of y is "HELLO". The output of
console.log(y) is "HELLO".
Answer: b) "53"
Explanation: In JavaScript, the + operator can be used for
addition or string concatenation, depending on the type of the
operands. If either operand is a string, the + operator performs
string concatenation. In this case, the value of x is a number and
the value of y is a string, so the + operator performs string
concatenation and the output of console.log(x + y) is "53".
var y = 5;
console.log((x > y) && (y < 6));
a) true
b) false
c) TypeError
d) ReferenceError
Answer: b) false
Explanation: In JavaScript, the && operator returns true if both
operands are true, and false otherwise. In this case, (x > y)
evaluates to true (because 10 is greater than 5), but (y < 6)
evaluates to false (because 5 is not less than 6). Therefore, the
expression (x > y) && (y < 6) evaluates to false. The output of
console.log((x > y) && (y < 6)) is false.
Answer: b) "53"
Explanation: In JavaScript, the + operator performs string
concatenation when either operand is a string. In this case, both
x and y are strings, so the + operator performs string
concatenation and the output of console.log(x + y) is "53".
Answer: b) "105"
Explanation: In JavaScript, the + operator performs string
concatenation when either operand is a string. In this case, y is a
string and x is a number, so the + operator performs string
concatenation and the output of console.log(x + y) is "105".
Answer: b) false
Explanation: In JavaScript, arrays are objects, and two arrays are
considered equal only if they refer to the same object. In this
case, x and y are two different arrays with the same elements, so
the expression x == y evaluates to false. The output of
console.log(x == y) is false.
Answer: a) 3.33
Explanation: In JavaScript, the / operator performs division of
two operands. When one or both operands are floating-point
numbers, the result is also a floating-point number. Therefore,
the expression x / y evaluates to 3.33333... (with repeating
decimals), but the output of console.log(x / y) is rounded to two
decimal places and displayed as 3.33.
Answer: D) Array
Explanation: In JavaScript, primitive data types are String,
Number, Boolean, null, undefined, and symbol. Array is not a
primitive data type; it is an object type.
Answer: B) pop()
Explanation: The pop() method removes the last element from an
array and returns that element. The push() method adds one or
more elements to the end of an array, while the shift() method
removes the first element from an array and returns that
element. The unshift() method adds one or more elements to the
beginning of an array.
foo();
console.log("a defined? " + (typeof a !==
'undefined'));
console.log("b defined? " + (typeof b !==
'undefined'));
A) a defined? true, b defined? true