menu

JavaScript Basics


1. What is the difference between a for loop and a while loop in JavaScript?

There is no difference between a for loop and a while loop.

A for loop is used for counting and iterating over a range of values, while a while loop is used for executing a block of code while a condition is true.

A while loop is used for counting and iterating over a range of values, while a for loop is used for executing a block of code while a condition is true.

A for loop is used for executing a block of code while a condition is true, while a while loop is used for counting and iterating over a range of values.


2. What does NaN stand for in JavaScript?

Not a Name

Not a Number

Not a Null

Not a Boolean


3.

What is the output of the following code?

console.log(3 + "2");

5

32

6

23


4. What is a callback function in JavaScript?

A function that is called when an error occurs.

A function that is called when a button is clicked.

A function that is passed as an argument to another function and is called when the first function has finished executing.

A function that is used to generate random numbers.


5. Which of the following is a valid way to create a function in JavaScript?

function myFunction() {}

var myFunction = function() {};

const myFunction = () => {};

all of the above


6. Which of the following is a valid way to declare a variable in JavaScript?

variable name = value;

var name = value;

let name = value;

all of the above


7.

Which of the following is not a valid way to declare a variable in JavaScript?

var myVar = 5;

let myVar = 10;

const myVar = "hello";

myVar = true;


8. What is the difference between == and === in JavaScript?

There is no difference between == and ===.

== performs a strict comparison, while === performs a loose comparison.

=== performs a strict comparison, while == performs a loose comparison.

== and === both perform a strict comparison.


9. What is the difference between let and var in JavaScript?

There is no difference between let and var.

let is block-scoped, while var is function-scoped.

var is block-scoped, while let is function-scoped.

let can only be used to declare global variables.


10.

What is the output of the following code?

console.log(2 + 2 + "2");

42

22

221

4