In this post, we will see list of javascript interview questions
What is the difference between == and === ?
==
compares values for equality after type coercion, meaning that if the two values being compared have different types, JavaScript will try to convert them to a common type before making the comparison. For example,"5" == 5
would evaluate totrue
.===
, on the other hand, compares values for equality without type coercion. This means that"5" === 5
would evaluate tofalse
as the values have different types.- It is generally recommended to use
===
instead of==
in JavaScript, as it avoids unexpected type coercion and makes the code more predictable.
What are the differences between let, var and const ?
Here are the differences between let
and other ways of declaring variables in JavaScript:
let
is a keyword in JavaScript that is used to declare variables with block scopevar
: Variables declared withvar
have function scope, meaning they are accessible within the entire function in which they are declared, regardless of block scope.const
: Variables declared withconst
have block scope, likelet
, but their value cannot be re-assigned after declaration. This makes them useful for declaring constants.
Example:
if (true) {
var x = "var";
let y = "let";
const z = "const";
}
console.log(x); // "var"
console.log(y); // ReferenceError: y is not defined
console.log(z); // ReferenceError: z is not defined
In this example, var
is accessible outside the if block, while let
and const
are not, as they are only accessible within the block they are declared in.
What is the difference between class and an object in JavaScript?
Please go through this post for more details : https://www.javasavvy.com/javascript-class-and-object/
What is the use of ‘this’ keyword and how does it work?
Please go through this post for answer : https://www.javasavvy.com/java-script-this-keyword/
Explain hoisting in JavaScript?
Please go through this post for answer: https://www.javasavvy.com/javascript-hoisting-in-details-with-examples/
What is closure in JavaScript and how it works?
Please go through this post for answer : https://www.javasavvy.com/javascript-closure-example/
What is an anonymous function and how to use it in JavaScript?
Please go through this post for answer :https://www.javasavvy.com/javascript-anonymous-function/
How do you handle errors in JavaScript?
Errors in JavaScript can be handled using try...catch
statements. The try
block contains code that may throw an error, while the catch
block contains code that will handle the error if it occurs.
Here’s an example of how to use try...catch
statements in JavaScript:
try {
// code that may throw an error
let x = y + 1;
} catch (error) {
// code to handle the error
console.error(error);
}
What is the use of typeof operator in JavaScript?
The typeof
operator in JavaScript is used to determine the data type of a given value.
typeof “Hello World ==> String
tyoeof 50 ==> number
How to declare an array in JavaScript and what are the different methods to add elements to an array?
What is the difference between null and undefined in JavaScript?
https://www.javasavvy.com/javascript-difference-between-null-and-undefined/
Thanks for Reading.. Please visit below link for Javascript advanced Interview Questions
https://www.javasavvy.com/javascript-advanced-interview-questions/