10 JavaScript Code Interview questions with code example and detailed explanations

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

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

What is the difference between an object and an array in JavaScript?

What is the difference between a function declaration and a function expression in JavaScript?

What is the difference between a closure and a prototype in JavaScript?

What is the difference between a constructor function and an ordinary function in JavaScript?

What is the difference between a string and a number in JavaScript?

What is the difference between a boolean and an undefined value in JavaScript?

What is the difference between an error and an exception in JavaScript?

What is the difference between a callback function and a promise in JavaScript?

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

== and === are both equality operators in JavaScript, but they have different meanings. == checks for value equality, while === checks for value and type equality. This means that == will return true even if the two operands are of different types, as long as their values are equal. For example, 1 == “1” will return true, even though 1 and “1” are of different types. ===, on the other hand, will only return true if the two operands are of the same type and have the same value. For example, 1 === “1” will return false, because 1 and “1” are of different types.

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

var, let, and const are all keywords used to declare variables in JavaScript. However, they have different scopes and lifetimes. var variables have global scope by default, unless they are declared inside a function, in which case they have function scope. let variables have block scope, which means that they are only visible within the block in which they are declared. const variables are declared as constants, and their values cannot be changed once they are set.

What is the difference between an object and an array in JavaScript?

An object is a data structure that stores data in key-value pairs. An array is a data structure that stores data in a linear sequence. Objects are more flexible than arrays, because they can store any type of data, while arrays can only store data of the same type. However, arrays are more efficient than objects, because they can be accessed more quickly.

What is the difference between a function declaration and a function expression in JavaScript?

A function declaration is a statement that defines a function. A function expression is an expression that evaluates to a function. Function declarations are hoisted to the top of their scope, while function expressions are not. This means that a function declaration can be called before it is defined, while a function expression cannot.

What is the difference between a closure and a prototype in JavaScript?

A closure is a function that has access to the variables in its enclosing scope, even after the enclosing scope has been closed. A prototype is a blueprint for an object. It defines the properties and methods that all objects of that type will have.

What is the difference between a constructor function and an ordinary function in JavaScript?

A constructor function is a function that is used to create new objects. An ordinary function is a function that is not used to create new objects. Constructor functions are called with the new keyword, while ordinary functions are called without the new keyword.

What is the difference between a string and a number in JavaScript?

A string is a sequence of characters, while a number is a value that represents a quantity. Strings are enclosed in quotes, while numbers are not. Strings can be used to represent text, while numbers can be used to represent quantities.

What is the difference between a boolean and an undefined value in JavaScript?

A boolean is a value that can be either true or false. An undefined value is a value that does not exist. Booleans are used to represent logical values, while undefined values are used to represent the absence of a value.

What is the difference between an error and an exception in JavaScript?

An error is an unexpected event that disrupts the normal flow of execution. An exception is a type of error that can be handled by the program. Errors are thrown by the JavaScript interpreter, while exceptions are thrown by the program.

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. A promise is a data structure that represents the eventual completion of an asynchronous operation. Callback functions are executed when the asynchronous operation completes, while promises are resolved or rejected when the asynchronous operation completes.