0% found this document useful (0 votes)
32K views52 pages

100 JavaScript Questions

The document contains 100+ JavaScript interview questions and answers. Some key differences highlighted include: - Null represents an empty value while undefined represents a non-initialized value. - The "use strict" directive enables strict mode for stricter parsing and error handling. - A function declaration is hoisted while an expression is not, and is assigned to a variable. - Synchronous code blocks until complete while asynchronous code allows simultaneous execution. - The "this" keyword refers to the current object context to access properties and methods. - "Var" has function scope, "let" has block scope, and "const" cannot be reassigned. - A closure retains

Uploaded by

Miki Smith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
32K views52 pages

100 JavaScript Questions

The document contains 100+ JavaScript interview questions and answers. Some key differences highlighted include: - Null represents an empty value while undefined represents a non-initialized value. - The "use strict" directive enables strict mode for stricter parsing and error handling. - A function declaration is hoisted while an expression is not, and is assigned to a variable. - Synchronous code blocks until complete while asynchronous code allows simultaneous execution. - The "this" keyword refers to the current object context to access properties and methods. - "Var" has function scope, "let" has block scope, and "const" cannot be reassigned. - A closure retains

Uploaded by

Miki Smith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 52

1

100+ JavaScript Questions

What is the difference between null and undefined in


JavaScript?

What is the purpose of the "use strict" directive in JavaScript?

What is the difference between a function declaration and a


function expression in JavaScript?

What is the difference between synchronous and asynchronous


code execution in JavaScript?

What is the purpose of the "this" keyword in JavaScript?

What is the difference between "var", "let", and "const" in


JavaScript?

What is a closure in JavaScript?

Laurence Svekis https://basescripts.com/


2

What is the difference between a for loop and a for...in loop in


JavaScript?

What is the difference between call() and apply() in JavaScript?

What is the difference between null and undefined in


JavaScript?

What is a closure in JavaScript and how does it work?

What is hoisting in JavaScript?

How can you check if a variable is an array in JavaScript?

What is the difference between let and var in JavaScript?

What is the this keyword in JavaScript?

How do you declare a function in JavaScript?

What is an immediately-invoked function expression (IIFE) in


JavaScript?

What is event bubbling in JavaScript?

What is JavaScript?

What are the primitive data types in JavaScript?

What is the difference between let, const, and var in


JavaScript?

What is the difference between the "==" and "===" operators


in JavaScript?

What is the difference between a synchronous and


asynchronous function in JavaScript?

Laurence Svekis https://basescripts.com/


3

What is the difference between a for loop and a forEach loop in


JavaScript?

What is the difference between let and var in terms of hoisting?

What is the difference between call, apply, and bind in


JavaScript?

What is the difference between a callback function and a


promise in JavaScript?

What is a variable in JavaScript?

How do you declare a variable in JavaScript?

What is the difference between "let" and "var" in JavaScript?

How do you declare a constant variable in JavaScript?

What is a function in JavaScript?

How do you declare a function in JavaScript?

What is an array in JavaScript?

How do you loop through an array in JavaScript?

What is an object in JavaScript?

What is the difference between null and undefined in


JavaScript?

What is a closure in JavaScript?

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

How do you check if a variable is an array in JavaScript?

What is hoisting in JavaScript?

How do you create a new object in JavaScript?

Laurence Svekis https://basescripts.com/


4

What is the difference between let, const, and var in


JavaScript?

How do you loop through an object in JavaScript?

How do you prevent event bubbling in JavaScript?

How do you create a new array from an existing array in


JavaScript?

What is the difference between == and === operators in


JavaScript?

What is the this keyword in JavaScript?

What is the purpose of the let keyword in JavaScript?

How do you check if a variable is an array in JavaScript?

What is the difference between let and const in JavaScript?

What is the purpose of the map() method in JavaScript?

What is the difference between null and undefined in


JavaScript?

What is a promise in JavaScript?

What is the difference between synchronous and asynchronous


code in JavaScript?

What is JavaScript, and what are its key features?

What is the difference between let and var in JavaScript?

What is a closure in JavaScript, and how does it work?

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

Laurence Svekis https://basescripts.com/


5

What is the difference between null and undefined in


JavaScript?

What is a callback function in JavaScript?

What is the difference between synchronous and asynchronous


programming in JavaScript?

What is the difference between let and var in JavaScript?

What is NaN in JavaScript?

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

What is hoisting in JavaScript?

What is the difference between null and undefined in


JavaScript?

What is the difference between synchronous and asynchronous


code execution in JavaScript?

What is the difference between call and apply in JavaScript?

What is a callback function in JavaScript?

What is the difference between local storage and session


storage in JavaScript?

What are the different data types in JavaScript?

How do you declare a variable in JavaScript?

What is the difference between var, let, and const in


JavaScript?

How do you write a conditional statement in JavaScript?

What is a callback function in JavaScript?

Laurence Svekis https://basescripts.com/


6

How do you iterate over an array in JavaScript?

What is a closure in JavaScript?

What is the difference between null and undefined in


JavaScript?

What is the difference between "==" and "===" in JavaScript?

What is the difference between let and var in JavaScript?

What is the difference between null and undefined in


JavaScript?

What is hoisting in JavaScript?

What is the difference between a for loop and a forEach loop in


JavaScript?

What is the difference between synchronous and asynchronous


code in JavaScript?

What is a callback function in JavaScript?

What is event bubbling in JavaScript?

What is the difference between the DOM and the BOM in


JavaScript?

What is the difference between "undefined" and "null" in


JavaScript?

How do you declare a variable in JavaScript?

How do you check if a variable is an array in JavaScript?

How do you check if an object has a specific property in


JavaScript?

Laurence Svekis https://basescripts.com/


7

How do you convert a string to a number in JavaScript?

What is the difference between "==" and "===" in JavaScript?

What is the difference between "let" and "var" in JavaScript?

How do you create a new object in JavaScript?

What is the difference between null and undefined in


JavaScript?

What is the difference between let and var in JavaScript?

What is the use of ‘use strict’ in JavaScript?

What is a callback function in JavaScript?

What is an immediately-invoked function expression (IIFE) in


JavaScript?

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

What is the difference between synchronous and asynchronous


programming?

What is a closure in JavaScript?

What is the difference between let and const in JavaScript?

What is the difference between == and === operators in


JavaScript?

What is the difference between let and var in JavaScript?

What is the difference between null and undefined in


JavaScript?

What is a closure in JavaScript?

What is a callback function in JavaScript?

Laurence Svekis https://basescripts.com/


8

What is the difference between let and const in JavaScript?

What is the difference between map() and forEach() in


JavaScript?

What is the difference between null and undefined in


JavaScript?

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

What are the primitive data types in JavaScript?

What is a closure in JavaScript?

What is the difference between let, const, and var in


JavaScript?

What is the this keyword in JavaScript?

What is event bubbling in JavaScript?

What is the difference between synchronous and asynchronous


code in JavaScript?

What is the difference between map() and forEach() in


JavaScript?

What is a promise in JavaScript?

Laurence Svekis https://basescripts.com/


9

ANSWERS

What is the difference between null and undefined in


JavaScript?

null represents a deliberate non-value or empty object reference,


while undefined represents an uninitialized or missing value.

What is the purpose of the "use strict" directive in


JavaScript?

The "use strict" directive is used to enable strict mode, which


enforces stricter parsing and error handling rules, disallows the
use of certain features, and prevents certain common mistakes.

What is the difference between a function declaration and


a function expression in JavaScript?

A function declaration creates a named function that is hoisted to


the top of its scope, while a function expression creates an
unnamed function that is assigned to a variable or property.

What is the difference between synchronous and


asynchronous code execution in JavaScript?

Synchronous code is executed in a sequential order, blocking the


program until a task is completed, while asynchronous code

Laurence Svekis https://basescripts.com/


10

allows multiple tasks to be executed simultaneously and


continues running without waiting for a task to complete.

What is the purpose of the "this" keyword in JavaScript?

The "this" keyword refers to the current object context and allows
methods to access and manipulate properties and methods of the
object that called them.

What is the difference between "var", "let", and "const" in


JavaScript?

"var" declares a variable with function scope, "let" declares a


variable with block scope, and "const" declares a variable that
cannot be reassigned.

What is a closure in JavaScript?

A closure is a function that has access to variables in its outer


lexical scope, even after the outer function has returned.
A closure is a function that has access to variables in its outer
(enclosing) function's scope. This allows the function to
"remember" the values of those variables, even if they are no
longer in scope.

Laurence Svekis https://basescripts.com/


11

What is the difference between a for loop and a for...in


loop in JavaScript?

A for loop iterates over a range of values, while a for...in loop


iterates over the properties of an object.

What is the difference between call() and apply() in


JavaScript?

Both methods allow you to call a function with a given this value
and arguments, but while call() accepts an argument list, apply()
accepts an array of arguments.

What is the difference between null and undefined in


JavaScript?

null is a value that represents the intentional absence of any


object value, while undefined is a value assigned to variables that
have not been assigned a value.

What is a closure in JavaScript and how does it work?

A closure is a function that has access to its outer function scope,


even after the outer function has returned. It works by creating
an inner function that retains access to variables in the outer
function's scope.

Laurence Svekis https://basescripts.com/


12

What is hoisting in JavaScript?

Hoisting is a behavior in JavaScript where variable and function


declarations are moved to the top of their scope, allowing them to
be used before they are declared.

How can you check if a variable is an array in JavaScript?

You can use the Array.isArray() method to check if a variable is an


array. It returns true if the variable is an array, and false
otherwise.

What is the difference between let and var in JavaScript?

let declares a block-scoped variable that can be reassigned, while


var declares a variable with function scope that can be
reassigned.

What is the this keyword in JavaScript?

The this keyword refers to the current object in which the code is
being executed. The value of this depends on how the function is
called.

How do you declare a function in JavaScript?

You can declare a function using the function keyword followed by


the function name, a set of parentheses for parameters, and a
block of code inside curly braces. For example:

Laurence Svekis https://basescripts.com/


13

function myFunction(param1, param2) {


// code goes here
}

What is an immediately-invoked function expression


(IIFE) in JavaScript?

An IIFE is a function that is immediately executed when it is


defined. It is typically used to create a private scope and avoid
polluting the global namespace. For example:
(function() {
// code goes here
})();

What is event bubbling in JavaScript?

Event bubbling is a behavior in JavaScript where events triggered


on an element propagate up the DOM tree to all its ancestor
elements. This allows for event delegation, where events are
handled on ancestor elements instead of individual child
elements.

What is JavaScript?

JavaScript is a programming language that is primarily used to


create interactive web pages and dynamic user interfaces. It is a

Laurence Svekis https://basescripts.com/


14

high-level, object-oriented language that can be used on both the


front-end and back-end of web development.
JavaScript is a scripting language that allows web developers to
create dynamic and interactive websites. It runs on the client-side
of a web page, enabling it to communicate with the server-side
and make changes to the web page without requiring a page
refresh.
JavaScript is a programming language that is widely used to
create interactive and dynamic web content. It is a high-level,
object-oriented language that can be run on almost any platform.

What are the primitive data types in JavaScript?

The primitive data types in JavaScript are string, number,


boolean, null, undefined, and symbol (added in ES6).

What is the difference between let, const, and var in


JavaScript?

let and const are block-scoped variables introduced in ES6, while


var is function-scoped. let variables can be reassigned, while
const variables cannot be reassigned. Additionally, const variables
must be assigned a value when they are declared, while let
variables can be assigned a value later.

Laurence Svekis https://basescripts.com/


15

What is the difference between the "==" and "==="


operators in JavaScript?

The "==" operator compares two values for equality, converting


them to a common type if necessary, while the "===" operator
compares two values for strict equality, without type coercion.

== is a loose equality operator that compares values without


considering their data types. === is a strict equality operator
that compares values while considering their data types. For
example, "1" == 1 would return true, while "1" === 1 would
return false.

== compares values for equality after converting them to a


common type, while === compares values for equality without
type conversion.

What is the difference between a synchronous and


asynchronous function in JavaScript?

Synchronous functions execute one after the other, blocking the


execution of other code until they are finished. Asynchronous
functions can execute concurrently with other code, allowing
multiple tasks to be performed at the same time.

Laurence Svekis https://basescripts.com/


16

What is the difference between a for loop and a forEach


loop in JavaScript?

A for loop is a traditional looping construct that allows you to


iterate over an array or object by specifying a starting point, an
ending point, and an increment. A forEach loop is a built-in
method on arrays that iterates over each element in the array
and performs a function on each element.

What is the difference between let and var in terms of


hoisting?

Variables declared with var are hoisted to the top of their scope,
meaning that they can be accessed before they are declared.
Variables declared with let are not hoisted, and attempting to
access them before they are declared will result in a reference
error.

What is the difference between call, apply, and bind in


JavaScript?

call and apply are methods that allow you to call a function with a
specific context (i.e., the value of the this keyword) and
arguments. The difference between them is how you pass the

Laurence Svekis https://basescripts.com/


17

arguments: call accepts arguments as a comma-separated list,


while apply accepts arguments as an array. bind is a method that
returns a new function with a bound context, allowing you to call
the function later with the same context.

What is the difference between a callback function and a


promise in JavaScript?

A callback function is a function that is passed as an argument to


another function and is called when that function completes. A
promise is an object that represents the eventual completion (or
failure) of an asynchronous operation, and provides a way to
handle the result of that operation. Promises are typically used to
avoid "callback hell" and make asynchronous code easier to read
and maintain.

What is a variable in JavaScript?

A variable is a named storage location in JavaScript that holds a


value. It is used to store data that can be accessed and
manipulated throughout a script.

Laurence Svekis https://basescripts.com/


18

How do you declare a variable in JavaScript?

You can declare a variable in JavaScript using the "var", "let", or


"const" keyword followed by the variable name. For example:
var myVariable;
let myVariable;
const myVariable;

What is the difference between "let" and "var" in


JavaScript?

"let" and "var" both declare variables in JavaScript, but "let" is


block-scoped, meaning it is only accessible within the block it was
declared in, while "var" is function-scoped, meaning it is
accessible throughout the entire function it was declared in.

How do you declare a constant variable in JavaScript?

You can declare a constant variable in JavaScript using the


"const" keyword followed by the variable name. For example:
const myConstant = 10;
Once a value is assigned to a constant variable, it cannot be
reassigned.

Laurence Svekis https://basescripts.com/


19

What is a function in JavaScript?

A function is a block of code that performs a specific task in


JavaScript. It can be reused multiple times throughout a script
and can accept input parameters and return values.

How do you declare a function in JavaScript?

You can declare a function in JavaScript using the "function"


keyword followed by the function name, input parameters
(optional), and the function code. For example:

function myFunction(param1, param2) {


// function code here
}

What is an array in JavaScript?

An array is a collection of data in JavaScript that can hold multiple


values, including strings, numbers, and objects. It is declared
using square brackets and each value is separated by a comma.
For example:
let myArray = ["apple", "banana", "orange"];

Laurence Svekis https://basescripts.com/


20

How do you loop through an array in JavaScript?

You can loop through an array in JavaScript using a "for" loop or a


"forEach" loop. For example:
// for loop
for (let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}

// forEach loop
myArray.forEach(function(item) {
console.log(item);
});

What is an object in JavaScript?

An object is a collection of properties in JavaScript that can hold


multiple values, including strings, numbers, arrays, and other
objects. It is declared using curly braces and each property is
separated by a comma. For example:
let myObject = {
name: "John",
age: 30,
hobbies: ["reading", "traveling"]
};

Laurence Svekis https://basescripts.com/


21

What is the difference between null and undefined in


JavaScript?

null is an explicitly assigned value that represents no value or an


empty object, while undefined is a variable that has been
declared but has not been assigned a value.

What is a closure in JavaScript?

A closure is a function that has access to variables in its outer


scope, even after the outer function has returned.
Answer: A closure is a function that has access to variables in its
outer scope, even after the outer function has returned. This
allows for more powerful and flexible coding patterns.

What is the difference between == and === in


JavaScript?

== performs type coercion before comparison, while === does


not. For example, "5" == 5 would be true because the string "5"
is coerced to the number 5, but "5" === 5 would be false.

How do you check if a variable is an array in JavaScript?

You can use the Array.isArray() method to check if a variable is an


array. For example, Array.isArray([1, 2, 3]) would return true.

Laurence Svekis https://basescripts.com/


22

What is hoisting in JavaScript?

Hoisting is a behavior in JavaScript where variable and function


declarations are moved to the top of their scope before code
execution.

How do you create a new object in JavaScript?

You can create a new object in JavaScript using either object


literal notation (const obj = {}) or the Object() constructor (const
obj = new Object()).

What is the difference between let, const, and var in


JavaScript?

let and const are block-scoped and cannot be redeclared in the


same scope, while var is function-scoped and can be redeclared in
the same scope. const is also a constant variable that cannot be
reassigned.

How do you loop through an object in JavaScript?

You can use a for...in loop to iterate over the keys of an object.
For example:
const obj = { a: 1, b: 2, c: 3 };
for (const key in obj) {
console.log(key, obj[key]);
}

Laurence Svekis https://basescripts.com/


23

How do you prevent event bubbling in JavaScript?

You can use the stopPropagation() method of the event object to


prevent event bubbling. For example:
const button = document.querySelector('button');
button.addEventListener('click', (event) => {
event.stopPropagation();
// Do something
});

How do you create a new array from an existing array in


JavaScript?

You can use methods like map(), filter(), and reduce() to create a
new array from an existing array. For example:
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]

What is the difference between == and === operators in


JavaScript?

Answer: The == operator compares the values of two operands


for equality, while the === operator compares both the values
and types of the operands. For example, 5 == "5" would be true,
but 5 === "5" would be false.

Laurence Svekis https://basescripts.com/


24

What is the this keyword in JavaScript?

Answer: this refers to the object that the function is a method of.
It is determined at runtime and can change depending on how
the function is called.

What is the purpose of the let keyword in JavaScript?

Answer: let is used to declare a block-scoped variable in


JavaScript. It is similar to var, but the scope of the variable is
limited to the block in which it is declared.

How do you check if a variable is an array in JavaScript?

Answer: You can use the Array.isArray() method to check if a


variable is an array. For example, Array.isArray([1, 2, 3]) would
be true.

What is the difference between let and const in


JavaScript?

Answer: let is used to declare a variable that can be reassigned,


while const is used to declare a variable that cannot be
reassigned. However, the value of a const variable can still be
changed if it is an object or an array.

Laurence Svekis https://basescripts.com/


25

What is the purpose of the map() method in JavaScript?

Answer: The map() method is used to create a new array by


applying a function to each element of an existing array. The
original array is not modified.

What is the difference between null and undefined in


JavaScript?

Answer: null is a value that represents the intentional absence of


any object value, while undefined is a value that represents the
absence of any value. undefined is the default value of a variable
that has not been assigned a value.

What is a promise in JavaScript?

Answer: A promise is an object that represents a value that may


not be available yet but will be in the future. It allows you to write
asynchronous code that is more readable and maintainable.

What is the difference between synchronous and


asynchronous code in JavaScript?

Answer: Synchronous code executes in a sequential order,


meaning that one line of code is executed at a time and the next

Laurence Svekis https://basescripts.com/


26

line of code cannot begin until the previous line has finished.
Asynchronous code does not execute in a sequential order,
meaning that multiple lines of code can be executed at the same
time. Asynchronous code is often used for tasks that may take a
long time to complete, such as retrieving data from a server.

What is JavaScript, and what are its key features?

JavaScript is a high-level, dynamic, and interpreted programming


language that is used to create interactive and dynamic web
pages. Some of its key features include:

Object-oriented programming support


Dynamic typing
First-class functions
Event-driven programming
Asynchronous programming
Cross-platform compatibility
Easy integration with HTML and CSS

What is the difference between let and var in JavaScript?

The let and var keywords are used to declare variables in


JavaScript, but they have some important differences:

Laurence Svekis https://basescripts.com/


27

var is function-scoped, meaning that it is accessible within the


function in which it is declared. let is block-scoped, meaning that
it is only accessible within the block in which it is declared (such
as an if statement or for loop).
Variables declared with var are hoisted to the top of their function
or global scope, meaning that they are accessible even before
they are declared. Variables declared with let are not hoisted.
var allows redeclaration of the same variable within the same
scope, whereas let does not.

What is a closure in JavaScript, and how does it work?

A closure is a function that has access to variables in its outer


(enclosing) function scope, even after the outer function has
returned. In other words, a closure allows a function to access
and manipulate variables that are not in its own scope.
Closures work by creating a reference to the outer function's
variables when the inner function is created. This reference is
stored in memory and is available to the inner function even after
the outer function has completed.

Laurence Svekis https://basescripts.com/


28

What is the difference between == and === in


JavaScript?

The double equals (==) and triple equals (===) operators are
used to compare values in JavaScript. The key difference between
them is that == performs type coercion, while === does not.

When using ==, JavaScript will try to convert the operands to the
same type before performing the comparison. For example, 1 ==
'1' would return true, because JavaScript converts the string '1' to
a number before comparing.

When using ===, the operands must be of the same type to be


considered equal. 1 === '1' would return false, because the
types (number and string) are different.

What is the difference between null and undefined in


JavaScript?

null and undefined are both values that represent the absence of
a value in JavaScript, but they have different meanings:
undefined is a value that is assigned to a variable that has not
been assigned a value or has been assigned the value undefined
explicitly. It is also returned by functions that do not have a
return statement.

Laurence Svekis https://basescripts.com/


29

null is a value that represents the intentional absence of any


object value. It is often used to indicate that a variable or object
property should have no value.

What is a callback function in JavaScript?

A callback function is a function that is passed as an argument to


another function and is executed after that function has
completed. Callback functions are often used in asynchronous
programming, where a function needs to wait for a result before
it can continue executing.

What is the difference between synchronous and


asynchronous programming in JavaScript?

Synchronous programming refers to code that is executed


sequentially, one line at a time, and each line of code blocks until
the previous line has completed. Asynchronous programming, on
the other hand, allows code to be executed out of order, so that
other operations can occur while waiting for an operation to
complete.
Asynchronous programming in JavaScript is often achieved using
callbacks, promises, or async/await.

Laurence Svekis https://basescripts.com/


30

What is the difference between let and var in JavaScript?

Answer: The main difference between let and var is their scope.
Variables declared with var are function-scoped, while variables
declared with let are block-scoped.

What is NaN in JavaScript?

Answer: NaN stands for "Not a Number". It is a special value that


represents the result of an operation that cannot produce a valid
number.

What is the difference between == and === in


JavaScript?

Answer: The == operator checks for equality, but it performs


type coercion, meaning that it tries to convert the values to a
common type before comparing them. The === operator, on the
other hand, checks for strict equality, meaning that it does not
perform type coercion and requires both the value and the type to
be the same.

Laurence Svekis https://basescripts.com/


31

What is hoisting in JavaScript?

Answer: Hoisting is a JavaScript mechanism where variables and


function declarations are moved to the top of their respective
scopes. This means that you can use them before they are
declared.

What is the difference between null and undefined in


JavaScript?

Answer: Both null and undefined represent absence of a value,


but null is an assigned value that represents no value or an
empty object pointer, while undefined represents a variable that
has been declared but has not been assigned a value.

What is the difference between synchronous and


asynchronous code execution in JavaScript?

Answer: Synchronous code execution means that each line of


code is executed one after another, in order. Asynchronous code
execution means that some code can be run in the background
while other code is still executing, allowing multiple tasks to be
performed simultaneously.

Laurence Svekis https://basescripts.com/


32

What is the difference between call and apply in


JavaScript?

Answer: Both call and apply are used to call a function with a
given this value, but call takes individual arguments, while apply
takes an array of arguments.

What is a callback function in JavaScript?

Answer: A callback function is a function that is passed as an


argument to another function, and is executed when that function
completes its task.

What is the difference between local storage and session


storage in JavaScript?

Answer: Both local storage and session storage are mechanisms


for storing data on the client-side, but local storage persists even
after the browser is closed, while session storage is cleared when
the browser is closed.

What are the different data types in JavaScript?

JavaScript has seven different data types:


1. Number

Laurence Svekis https://basescripts.com/


33

2. String
3. Boolean
4. Null
5. Undefined
6. Object
7. Symbol

How do you declare a variable in JavaScript?

You can declare a variable in JavaScript using the var, let, or


const keyword. For example:
var myVariable = "Hello, World!";
let myOtherVariable = 42;
const myConstantVariable = true;

What is the difference between var, let, and const in


JavaScript?

The main difference between var, let, and const is how they
handle scope and reassignment:
● var: Variables declared with var have function scope or
global scope, and can be reassigned.
● let: Variables declared with let have block scope and can be
reassigned.
● const: Variables declared with const have block scope and
cannot be reassigned.

Laurence Svekis https://basescripts.com/


34

How do you write a conditional statement in JavaScript?

You can write a conditional statement in JavaScript using the if,


else if, and else keywords. For example:
if (x > 10) {
console.log("x is greater than 10");
} else if (x < 10) {
console.log("x is less than 10");
} else {
console.log("x is equal to 10");
}

What is a callback function in JavaScript?

A callback function is a function that is passed as an argument to


another function and is executed when the other function is
finished.

How do you iterate over an array in JavaScript?

You can iterate over an array in JavaScript using a for loop, a


forEach() loop, or a map() loop. For example:

let myArray = [1, 2, 3, 4, 5];

// Using a for loop

Laurence Svekis https://basescripts.com/


35

for (let i = 0; i < myArray.length; i++) {


console.log(myArray[i]);
}

// Using a forEach() loop


myArray.forEach(function(item) {
console.log(item);
});

// Using a map() loop


let mappedArray = myArray.map(function(item) {
return item * 2;
});
console.log(mappedArray);
What is the difference between == and === in JavaScript?
The double equals (==) operator checks for equality of value, but
it does not check for equality of type. The triple equals (===)
operator checks for both equality of value and equality of type.

What is a closure in JavaScript?

A closure is a function that has access to variables in its outer


(enclosing) function's scope chain.

Laurence Svekis https://basescripts.com/


36

Answer: A closure is a function that has access to variables in its


outer lexical environment, even after that environment has been
destroyed.
A closure is a function that has access to variables in its outer
function scope, even after the outer function has returned. This is
possible because the inner function "closes over" the variables in
the outer function. Closures are commonly used to create private
variables and functions in JavaScript.
A closure is a function that has access to its parent scope, even
after the parent function has closed.

What is the difference between null and undefined in


JavaScript?

Undefined is a variable that has been declared but has not been
assigned a value. Null is a value that represents no value or an
empty value.

What is the difference between "==" and "===" in


JavaScript?

The "==" operator checks for equality of values, but it does not
check for the data type. For example, 5 == "5" will return true.
On the other hand, the "===" operator checks for both equality
of values and data type. For example, 5 === "5" will return false.

Laurence Svekis https://basescripts.com/


37

What is the difference between let and var in JavaScript?

The main difference between let and var is that let has block
scope, while var has function scope. This means that variables
declared with let are only accessible within the block in which
they are defined, while variables declared with var are accessible
throughout the entire function in which they are defined.

What is the difference between null and undefined in


JavaScript?

In JavaScript, null is a value that represents the intentional


absence of any object value, while undefined is a value that
represents an uninitialized variable or an object property that
does not exist. In other words, null is a value that is explicitly set
by a programmer, while undefined is a value that is implicitly set
by the JavaScript engine.

What is hoisting in JavaScript?

Hoisting is a JavaScript mechanism where variables and function


declarations are moved to the top of their respective scopes
during compilation, regardless of where they are defined. This
means that you can use a variable or function before it has been
declared, as long as it is declared somewhere in the same scope.

Laurence Svekis https://basescripts.com/


38

What is the difference between a for loop and a forEach


loop in JavaScript?

A for loop is a traditional loop that iterates over a range of values,


while a forEach loop is a newer method that iterates over an
array and executes a function for each element. The main
difference is that a for loop can be used with any iterable object,
while a forEach loop is only used with arrays.

What is the difference between synchronous and


asynchronous code in JavaScript?

Synchronous code is code that is executed sequentially, one line


at a time, and blocks the execution until a particular operation is
completed. Asynchronous code, on the other hand, is code that
does not block the execution and allows other code to run while
waiting for an operation to complete. This is achieved through the
use of callbacks, promises, and async/await.

What is a callback function in JavaScript?

A callback function is a function that is passed as an argument to


another function and is executed after a certain operation is
completed. Callback functions are commonly used in
asynchronous programming to handle the results of an operation
once it is completed.

Laurence Svekis https://basescripts.com/


39

What is event bubbling in JavaScript?

Event bubbling is a phenomenon where an event that occurs on a


nested element is propagated up through its parent elements in
the DOM tree. This means that if you click on a nested element,
the click event will also trigger on all of its parent elements up to
the root element.

What is the difference between the DOM and the BOM in


JavaScript?

The Document Object Model (DOM) is a programming interface


for HTML and XML documents that allows JavaScript to access
and manipulate the content and structure of a web page. The
Browser Object Model (BOM), on the other hand, provides a
programming interface for the browser window and other
browser-specific features, such as history and location.

What is the difference between "undefined" and "null" in


JavaScript?

Undefined means a variable has been declared but has not yet
been assigned a value. Null is an assignment value that
represents no value or no object.

Laurence Svekis https://basescripts.com/


40

How do you declare a variable in JavaScript?

You can declare a variable in JavaScript using the "var", "let", or


"const" keywords followed by the variable name. For example:
var myVar;
let myLet;
const myConst;

How do you check if a variable is an array in JavaScript?

You can check if a variable is an array in JavaScript


using the Array.isArray() method. For example:
Array.isArray([1, 2, 3]); // true
Array.isArray("hello"); // false

How do you check if an object has a specific property in


JavaScript?

You can check if an object has a specific property in JavaScript


using the "in" operator or the hasOwnProperty() method. For
example:
"propertyName" in myObject; // true or false
myObject.hasOwnProperty("propertyName"); // true or false

How do you convert a string to a number in JavaScript?

You can convert a string to a number in JavaScript using the


Number() or parseInt() functions. For example:

Laurence Svekis https://basescripts.com/


41

Number("123"); // 123
parseInt("123"); // 123

What is the difference between "==" and "===" in


JavaScript?

"==" compares the values of two operands, while "==="


compares both the values and the data types of two operands.
What is the "this" keyword in JavaScript?
The "this" keyword refers to the object that the function is a
method of, or the global object if the function is not a method of
any object.

What is the difference between "let" and "var" in


JavaScript?

"var" is function-scoped, while "let" is block-scoped. This means


that variables declared with "var" are accessible within the
function they are declared in, while variables declared with "let"
are only accessible within the block they are declared in.

How do you create a new object in JavaScript?

You can create a new object in JavaScript using either object


literal notation or the Object() constructor. For example:
var myObject = {name: "John", age: 30};
var myObject = new Object(); myObject.name = "John";
myObject.age = 30;

Laurence Svekis https://basescripts.com/


42

What is the difference between null and undefined in


JavaScript?

In JavaScript, null represents the intentional absence of any


object value while undefined means a variable has been declared
but has not been assigned a value.

What is the difference between let and var in JavaScript?

Both let and var are used to declare variables in JavaScript, but
let is block-scoped while var is function-scoped. This means that
variables declared with var are accessible throughout the
function, while variables declared with let are only accessible
within the block where they are defined.

What is the use of ‘use strict’ in JavaScript?

‘use strict’ is a directive that is used to enforce stricter parsing


and error handling in JavaScript code. It helps to identify coding
errors and ensures that the code is executed in a secure manner.

Laurence Svekis https://basescripts.com/


43

What is a callback function in JavaScript?

A callback function is a function that is passed as an argument to


another function and is executed after some operation has been
completed. It is often used in asynchronous programming to
handle the completion of a task.

What is an immediately-invoked function expression


(IIFE) in JavaScript?

An IIFE is a function that is executed as soon as it is defined. It is


used to create a private scope for a block of code and to prevent
the pollution of the global namespace.

Example:

(function() {
// Code to be executed
})();

Laurence Svekis https://basescripts.com/


44

What is the difference between == and === in


JavaScript?

The double equals (==) is used to compare values while ignoring


their data types. The triple equals (===) is used to compare
values while taking their data types into account.

What is the difference between synchronous and


asynchronous programming?

Synchronous programming is when code is executed one


statement at a time in a sequential manner. Asynchronous
programming allows code to be executed out of order and is often
used for operations that take a long time to complete.

What is a closure in JavaScript?

A closure is a function that has access to variables in its outer


function, even after that function has returned. It allows for data
encapsulation and helps to create private variables and functions.

Example:
function outerFunction() {
var outerVar = 'Hello, ';
function innerFunction(name) {

Laurence Svekis https://basescripts.com/


45

console.log(outerVar + name);
}
return innerFunction;
}
var innerFunc = outerFunction();
innerFunc('John'); // Output: Hello, John

What is the difference between let and const in


JavaScript?

Both let and const are used to declare variables in JavaScript, but
while let can be reassigned, const cannot. This means that once a
variable is declared with const, its value cannot be changed.

What is the difference between == and === operators in


JavaScript?

The double equals (==) operator compares the values of two


variables after type coercion, while the triple equals (===)
operator compares the values and types of two variables without
type coercion. For example:

console.log(5 == "5"); // true


console.log(5 === "5"); // false

Laurence Svekis https://basescripts.com/


46

What is the difference between let and var in JavaScript?

The var keyword declares a variable with function scope, while


the let keyword declares a variable with block scope. Block scope
means that the variable is only accessible within the block it is
defined in. For example:
function test() {
var a = 1;
let b = 2;
{
var a = 3;
let b = 4;
}
console.log(a); // 3
console.log(b); // 2
}

What is the difference between null and undefined in


JavaScript?

Undefined means a variable has been declared but has not yet
been assigned a value, while null is an assignment value
representing no value or no object. For example:

let a;
console.log(a); // undefined

Laurence Svekis https://basescripts.com/


47

let b = null;
console.log(b); // null

What is a closure in JavaScript?

A closure is a function that has access to its parent function's


variables, even after the parent function has returned. Closures
can be used to create private variables and functions. For
example:
function outer() {
let a = 1;
function inner() {
console.log(a);
}
return inner;
}

let innerFunc = outer();


innerFunc(); // 1

What is a callback function in JavaScript?

A callback function is a function that is passed as an argument to


another function and is executed when the parent function is
done executing. Callbacks are often used in asynchronous
programming to handle the results of an asynchronous operation.
For example:

Laurence Svekis https://basescripts.com/


48

function getData(callback) {
setTimeout(function() {
let data = "Some data";
callback(data);
}, 1000);
}

getData(function(data) {
console.log(data);
});

What is the difference between let and const in


JavaScript?

The const keyword declares a variable that cannot be reassigned,


while the let keyword declares a variable that can be reassigned.
Both let and const variables have block scope. For example:
const a = 1;
a = 2; // TypeError: Assignment to constant variable.

let b = 3;
b = 4;
console.log(b); // 4

Laurence Svekis https://basescripts.com/


49

What is the difference between map() and forEach() in


JavaScript?

The map() method creates a new array with the results of calling
a function on each element in the original array, while the
forEach() method executes a function on each element in the
original array. The map() method returns a new array, while the
forEach() method does not. For example:
let arr = [1, 2, 3];
let mappedArr = arr.map(function(elem) {
return elem * 2;
});
console.log(mappedArr); // [2, 4, 6]

arr.forEach(function(elem) {
console.log(elem * 2);
});

What is the difference between null and undefined in


JavaScript?

null and undefined are both used to represent missing or


nonexistent values, but they have different meanings. null is a
value that represents the intentional absence of any object value,

Laurence Svekis https://basescripts.com/


50

while undefined is a value that represents an uninitialized,


missing, or non-existent value.

What is the difference between == and === in


JavaScript?

== is a loose equality operator that only checks for value


equality, while === is a strict equality operator that checks for
both value and type equality.

What are the primitive data types in JavaScript?

There are six primitive data types in JavaScript: string, number,


boolean, null, undefined, and symbol.

What is a closure in JavaScript?

A closure is a function that has access to its outer function's


variables, even after the outer function has returned. This allows
for private variables and data encapsulation in JavaScript.

Laurence Svekis https://basescripts.com/


51

What is the difference between let, const, and var in


JavaScript?

let and const are block-scoped variables, while var is


function-scoped. let allows for reassignment of the variable, while
const does not.

What is the this keyword in JavaScript?

The this keyword refers to the object that the current function or
method is a property of. It is determined dynamically at runtime
based on how the function is called.

What is event bubbling in JavaScript?

Event bubbling is the process by which an event that is triggered


on an inner element will also trigger on all of its parent elements,
propagating up the DOM tree.

What is the difference between synchronous and


asynchronous code in JavaScript?

Synchronous code executes in a linear, blocking manner, meaning


that each statement is executed one after the other in the order
that they are written. Asynchronous code, on the other hand,

Laurence Svekis https://basescripts.com/


52

allows multiple tasks to be executed simultaneously, and does not


block the execution of other code while waiting for a task to
complete.

What is the difference between map() and forEach() in


JavaScript?

map() and forEach() are both array methods in JavaScript. The


main difference between them is that map() returns a new array,
while forEach() does not.

What is a promise in JavaScript?

A promise is an object that represents the eventual completion


(or failure) of an asynchronous operation and its resulting value.
It allows for better handling of asynchronous code and helps
avoid callback hell.

Laurence Svekis https://basescripts.com/

You might also like