JavaScript Basics Quiz

JavaScript
1 Passed
100% acceptance

A beginner-friendly quiz to evaluate your understanding of core JavaScript fundamentals including variables, data types, operators, and foundational syntax. This quiz builds the base for mastering more advanced JavaScript concepts.

40 Questions
~80 minutes
1

Question 1

What is the output of the following code?

javascript
console.log(typeof undefined)
A
undefined
B
object
C
string
D
null
2

Question 2

Which of the following declares a block-scoped variable?

A
var
B
let
C
function
D
define
3

Question 3

What will the following expression return?

javascript
"5" === 5
A
true
B
false
C
undefined
D
NaN
4

Question 4

What is the output of the following code?

javascript
console.log(2 + "3")
A
23
B
5
C
"5"
D
NaN
5

Question 5

Which statement best describes JavaScript?

A
A server-side language only
B
A compiled, strongly typed language
C
A dynamically typed, interpreted language
D
A markup language used for styling
6

Question 6

Which keyword declares a constant variable that cannot be reassigned?

A
let
B
const
C
var
D
static
7

Question 7

How do you write a template literal that outputs “Hello, John!” when name is “John”?

javascript
const name = "John"
// Fill the blank
const message = `Hello, ${_____}!`
A
name
B
"name"
C
${name}
D
Hello
8

Question 8

What is the default value of a variable declared with let but not assigned?

A
null
B
undefined
C
0
D
false
9

Question 9

Which array method creates a new array with results of calling a provided function on every element?

A
forEach()
B
map()
C
filter()
D
reduce()
10

Question 10

What will be logged?

javascript
const obj = { a: 1 }
const copy = obj
copy.a = 2
console.log(obj.a)
A
1
B
2
C
undefined
D
Throws an error
11

Question 11

Which of these values is falsy in JavaScript?

A
"0"
B
[]
C
0
D
{}
12

Question 12

Which statement properly checks if `person.age` exists before accessing it?

A
if (person.age)
B
if (person?.age)
C
if (person.age?)
D
if (person => age)
13

Question 13

What will `Array.isArray([])` return?

A
true
B
false
C
null
D
undefined
14

Question 14

How do you convert the string "42" to a number using shorthand syntax?

A
Number("42")
B
parseInt("42")
C
+ "42"
D
"42".toNumber()
15

Question 15

Which keyword is used to create a block scope?

A
for
B
if
C
let
D
switch
16

Question 16

What is the output of the following arrow function?

javascript
const add = (a, b) => a + b
console.log(add(2, 3))
A
5
B
undefined
C
NaN
D
Error
17

Question 17

Which method converts a JSON string into an object?

A
JSON.stringify()
B
JSON.parse()
C
JSON.object()
D
JSON.convert()
18

Question 18

What does the spread operator do in the following context?

javascript
const arr = [1, 2, 3]
const copy = [...arr]
A
Creates a shallow copy of arr
B
Creates a deep copy of arr
C
Converts arr to an object
D
Sorts arr automatically
19

Question 19

What will be logged?

javascript
console.log(1 == "1")
A
true
B
false
C
Throws TypeError
D
undefined
20

Question 20

What is the result of `typeof NaN`?

A
"number"
B
"NaN"
C
"undefined"
D
"object"
21

Question 21

How do you check if an array includes a specific value?

A
arr.has(value)
B
arr.includes(value)
C
arr.contains(value)
D
arr.any(value)
22

Question 22

What does the `trim()` method do on a string?

A
Removes whitespace from both ends
B
Removes whitespace from the start only
C
Removes whitespace from the end only
D
Removes all spaces within the string
23

Question 23

Which pattern describes an immediately invoked function expression?

A
(function() {})()
B
function() {}
C
const fn = () => {}
D
new Function()
24

Question 24

What will this code log?

javascript
const arr = [1, 2, 3]
arr.push(4)
console.log(arr.length)
A
3
B
4
C
undefined
D
Throws an error
25

Question 25

Which statement about strict mode is true?

A
It is enabled by default in all scripts
B
It prevents the use of undeclared variables
C
It allows duplicate parameter names
D
It disables all type coercion
26

Question 26

How do you create a Promise that resolves immediately with value 5?

A
new Promise(5)
B
Promise.resolve(5)
C
Promise.done(5)
D
Promise.then(5)
27

Question 27

What is logged?

javascript
let x
console.log(x === undefined)
A
true
B
false
C
Throws ReferenceError
D
null
28

Question 28

Which loop is best for iterating over object keys?

A
for (const key in obj)
B
for (const value of obj)
C
while(obj)
D
loop obj.keys
29

Question 29

How do you declare a default parameter in a function?

javascript
function greet(name = "Guest") {
  return `Hello, ${name}!`
}
A
Set default inside function body
B
Assign default in parameter list
C
Use name.default = value
D
You cannot set defaults
30

Question 30

What is the purpose of the `finally` block in a try/catch?

A
It only runs when an error occurs
B
It runs after try and catch regardless of the outcome
C
It stores the caught error
D
It restarts the try block
31

Question 31

What does `document.querySelector(".button")` return?

A
An array of all elements with class button
B
The first element with class button
C
An HTMLCollection of elements
D
null always
32

Question 32

Which operator is used for nullish coalescing?

A
??
B
||
C
&&
D
?:
33

Question 33

What will this log?

javascript
const value = "5"
console.log(value * 2)
A
"10"
B
10
C
NaN
D
Error
34

Question 34

Which method removes the last element from an array and returns it?

A
shift()
B
pop()
C
slice()
D
splice()
35

Question 35

What is the result of `[1, 2, 3].includes(2)`?

A
false
B
true
C
null
D
undefined
36

Question 36

Which statement about `setTimeout` is correct?

A
It blocks the main thread until finished
B
It executes the callback after a delay without blocking
C
It guarantees exact timing
D
It returns the result of the callback
37

Question 37

What danger exists when comparing objects directly with `===`?

A
It always returns true
B
It compares references, not structure
C
It converts objects to JSON
D
It throws a syntax error
38

Question 38

What is the correct way to destructure the `name` property from an object?

javascript
const user = { name: "Ada", age: 30 }
// Fill the blank
const { _____ } = user
A
name: userName
B
user.name
C
name
D
user = name
39

Question 39

Which statement properly creates a class with a constructor?

javascript
class Person {
  constructor(name) {
    this.name = name
  }
}
A
function Person(name) { this.name = name }
B
class Person { constructor(name) { this.name = name } }
C
const Person = () => { this.name = name }
D
class Person() { this.name = name }
40

Question 40

What will be logged?

javascript
const numbers = [1, 2, 3]
const [first, ...rest] = numbers
console.log(rest)
A
[2, 3]
B
[1]
C
[1, 2, 3]
D
[]

QUIZZES IN JavaScript