JAVASCRIPT INTERVIEW QUESTIONS IN 2022
What is DOM?
DOM stands for Document Object Model.
When the browser tries to render a HTML document, it creates an object based on the HTML document called DOM. Using this DOM, we can manipulate or change various elements inside the HTML document.
<html>
<head> <body>
<title> <p> <p>
DOM Example Hello Hey
What are arrow functions ?
Arrow functions were introduced in the ES6 version of javascript.
They provide us with a new and shorter syntax for declaring functions.
Arrow functions can only be used as a function expression.
***
// Traditional Function Expression
var add = function (a, b) {
return a + b;
}
// Arrow Function Expression
var arrowAdd = (a,b) ⇒ a + b;
Differences between declaring variables using var, let and const?
Before the ES6 version of javascript, only the keyword var was used to declare variables.
With the ES6 Version, keywords let and const were introduced to declare variables.
***
var variable 1 = 23;
let variable2 =89;
function catchValues (){
console.log (variable1) ;
console.log(variable2);
// Both the variables can be accessed anywhere since they are
declared in the global scope
}
window.variable1 ; // Returns the value 23
window.variable2; // Returns undefined
What is the use of promises in Javascript ?
Promises are used to handle asynchronous operations in javascript.
Promise object has four states-
- Pending – Initial state of promise. This state represents that the promise has neither been fulfilled nor been rejected, it is in the pending state.
- Fulfilled This state represents that the promise has been fulfilled, meaning the async operation is completed.
- Rejected – This state represents that the promise has been rejected for some reason, meaning the async operation has failed.
- Settled-This state represents that the promise has been either rejected or fulfilled.
What are classes in Javascript?
Introduced in the ES6 version, classes are nothing but syntactic sugars for constructor functions.
They provide a new way of declaring constructor functions in javascript.
***
// ES6 version classes
class Student{
constructor(name, rollNumber, grade, section){
this.name = name;
this.rolLNumber=
roll Number;
this.grade = grade;
this.section = section;
}
// Methods can be directly added inside the class
getDetails (){
return ‘Name: S{this.name), Roll no: S(this.rotlNumber), Grade :
S(this.grade), Section : S(this.section)’;
}
}
let student2 = new Student (“Garry”, 673, “7th”, “c”);
let student2= new student (“Garry”, 673, “7th”, “c”);
student 2.getDetails ();
// Returns Name: Garry, Roll no:673, Grade: 7th, Section:C
Introduced in the ES6 version, classes are nothing but syntactic sugars for constructor functions.
They provide a new way of declaring constructor functions in javascript.
***
// ES6 version classes
class Student{
constructor(name, rollNumber, grade, section){
this.name = name;
this.rolLNumber=
roll Number;
this.grade = grade;
this.section = section;
}
// Methods can be directly added inside the class
getDetails (){
return ‘Name: S{this.name), Roll no: S(this.rotlNumber), Grade :
S(this.grade), Section : S(this.section)’;
}
}
let student2 = new Student (“Garry”, 673, “7th”, “c”);
let student2= new student (“Garry”, 673, “7th”, “c”);
student 2.getDetails ();
// Returns Name: Garry, Roll no:673, Grade: 7th, Section:C
Introduced in the ES6 version, classes are nothing but syntactic
sugars for constructor functions.
They provide a new way of declaring constructor functions in javascript.
***
// ES6 version classes
class Student{
constructor(name, rollNumber, grade, section){
this.name = name;
this.rolLNumber=
roll Number;
this.grade = grade;
this.section = section;
}
// Methods can be directly added inside the class
getDetails (){
return ‘Name: S{this.name), Roll no: S(this.rotlNumber), Grade :
S(this.grade), Section : S(this.section)’;
}
}
let student2 = new Student (“Garry”, 673, “7th”, “c”);
let student2= new student (“Garry”, 673, “7th”, “c”);
student 2.getDetails ();
// Returns Name: Garry, Roll no:673, Grade: 7th, Section:C
What is a Temporal Dead Zone
Temporal Dead Zone is a behavior that occurs with variables declared using let and const keywords.
It is a behavior where we try to access a variable before it is initialized.
Example-
***
x = 23; // Gives reference error
let x;
function anotherRandomFunc (){
message = “Hello”; // Throws a reference error
let message;
}
What is the use of a constructor function in Javascript ?
Constructor functions are used to create objects in javascript.
If we want to create multiple objects having similar properties and methods, constructor functions are used.
***
function Person(name, age, gender) {
this.name = name;
this.age =age;
this.gender gender;
}
var person = new Person(“Vivek”, 76, “male”);
console.log (person1);
var person = new Person(“Courtney”, 34, “female”);
console.log(person2);
Whenever we want to create a new object of the type Person,
We need to create it using the new keyword:
***
var person = new Person (“Lilly”, 17, “female “) ;