In this article we will look into top 10 Javascript interview questions and their answers. JavaScript is a scripting language used for creating dynamic and interactive websites. It is an object-oriented language that can be run on both the client-side and server-side of the web.
Javascript interview questions
What is 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 is possible because the inner function retains a reference to its outer function’s variables. For example
function outer() {
var a = 1;
function inner() {
console.log(a);
}
return inner;
}
var innerFn = outer();
innerFn(); // logs 1
What is the difference between undefined
and null
in JavaScript?
undefined
means a variable has been declared but has not been assigned a value, whereas null
is an assignment value representing no value or no object. For example,
var a;
console.log(a); // logs undefined
var b = null;
console.log(b); // logs null
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,
var arr = [1, 2, 3];
console.log(Array.isArray(arr)); // logs true
var obj = { name: "John", age: 25 };
console.log(Array.isArray(obj)); // logs false
What is the difference between let
, const
, and var
in JavaScript?
var
declares a variable that is function-scoped, whereas let
and const
declare variables that are block-scoped. const
is used to declare constants that cannot be reassigned, whereas let
declares variables that can be reassigned. For example,
function test() {
var a = 1;
let b = 2;
const c = 3;
if (true) {
var a = 4; // function-scoped
let b = 5; // block-scoped
const c = 6; // block-scoped
console.log(a, b, c); // logs 4 5 6
}
console.log(a, b, c); // logs 4 2 3
}
What is the difference between ==
and ===
in JavaScript?
==
compares the values of two variables, whereas ===
compares the values and types of two variables. For example,
console.log(1 == "1"); // logs true
console.log(1 === "1"); // logs false
How do you create an object in JavaScript?
You can create an object using object literal notation, constructor function, or the Object.create()
method. For example,
// using object literal notation
var person = { name: "John", age: 25 };
// using constructor function
function Person(name, age) {
this.name = name;
this.age = age;
}
var person = new Person("John", 25);
// using Object.create() method
var person = Object.create(null);
person.name = "John";
person.age = 25;
What is the difference between let
and var
in terms of hoisting?
var
variables are hoisted to the top of their function or global scope, whereas let
variables are not hoisted. For example,
function test() {
console.log(a); // logs undefined
var a = 1;
console.log(b); // ReferenceError: b is not defined
let b = 2;
}
What is the difference between null
and undefined
in JavaScript?
undefined
means a variable has been declared but has not been assigned a value, whereas null
is an assignment value representing no value or no object. For example,
var a;
console.log(a); // logs undefined
var b = null;
console.log(b); // logs null
What is the difference between Object.keys()
and Object.getOwnPropertyNames()
in JavaScript?
Object.keys()
returns an array of the enumerable property names of an object, whereas Object.getOwnPropertyNames()
returns an array of all property names, including non enumerable ones. For example,
var obj = {
a: 1,
b: 2
};
console.log(Object.keys(obj)); // logs ["a", "b"]
console.log(Object.getOwnPropertyNames(obj)); // logs ["a", "b"]
Object.defineProperty(obj, "c", {
value: 3,
enumerable: false
});
console.log(Object.keys(obj)); // logs ["a", "b"]
console.log(Object.getOwnPropertyNames(obj)); // logs ["a", "b", "c"]
What is the difference between slice()
and splice()
in JavaScript?
slice()
returns a shallow copy of a portion of an array, whereas splice()
modifies the original array by adding or removing elements. For example,
var arr = [1, 2, 3, 4, 5];
var newArr = arr.slice(1, 3); // returns [2, 3]
console.log(newArr); // logs [2, 3]
arr.splice(1, 2, 6, 7); // modifies arr to [1, 6, 7, 4, 5]
console.log(arr); // logs [1, 6, 7, 4, 5]
Hope this helps you get clarified on some of the interview related question. Keep learning. 🙂
Further Reading