1. What is JavaScript?
A high-level, interpreted programming language called JavaScript makes it possible to create interactive web pages and online apps with dynamic functionality. Commonly referred to as the universal language, JavaScript is primarily used by developers for front-end and back-end work.
2. What are the different data types in JavaScript?
JavaScript has six primitive (in-built) data types:
(a) Number
(b) String
(c) Boolean
(d) Null
(e) Undefined
(f) Symbol
It also has two compound data types:
(a) Object
(b) Array
3. What is hoisting in JavaScript?
Hoisting is a JavaScript concept that refers to the process of moving declarations to the top of their scope. This means that variables and functions can be used before they are declared, as long as they are declared before they are used in a function.
For E.g. the following code will print “Hello, World!” even though the greet variable is not declared until after the console.log() statement.
var greet = "Hello, World!"
function sayHello(){
console.log(greet);
}
sayHello();
4. What is the purpose of the “this” keyword in JavaScript?
The “this” keyword refers to the object that is executing the current function or method. It allows access to object properties and methods within the context of that object.
const person = {
name: "Sayuj",
greet: function(){
console.log("Hello, " + this.name);
}
};
person.greet(); // Output: Hello, Sayuj
5. What is the difference between null and undefined?
null
is an assignment value that represents no value or an empty value, while undefined
is a variable that has been declared but not assigned a value.
var x;
console.log(x); // Output: undefined
var y = null;
console.log(y); // Output: null
6. Why do we use the word “debugger” in JavaScript?
The word “debugger”
is used in JavaScript to refer to a tool that can be used to step through JavaScript code line by line. This can be helpful for debugging JavaScript code, which is the process of finding and fixing errors in JavaScript code. To use the debugger
, you need to open the JavaScript console in your browser. Then, you can use debugger
commands to comb through your code line by line.
It’s essential to know debugging techniques as well as the more general ideas behind code optimization and speed improvement. In addition to operating smoothly, efficient code significantly enhances the user experience.
For example, the following code will print the value of the x variable at each step of the debugger
.
var x = 10;
debugger;
x=x+1;
debugger;
console.log(x);
7. What is the difference between == and === operators in JavaScript?
The equality ==
operator is a comparison operator that compares two values and returns true if they are equal. The strict equality ===
operator is also a comparison operator, but it compares two values and returns true only if they are equal and of the same type.
For example, the following code will return true, because the values of the x and y variables are equal.
var x = 10;
var y = 10;
var z = '10';
console.log(x==y); // Output: true
console.log(x===z); // Output: false
8. What is the difference between “var” and “let” keywords in JavaScript?
The var
and let
keywords are both used to declare variables in JavaScript. However, there are some key differences between the two keywords.
The var
keyword declares a global variable, which means that the variable can be accessed from anywhere in the code. The let
keyword declares a local variable, which means that the variable can only be accessed within the block of code where it is declared.
{
let x = 10;
console.log(x); // 10
}
9. What are closures in JavaScript?
Closures (closureFn
) are functions that have access to variables from an outer function even after the outer function has finished executing. They “remember” the environment in which they were created.
functions outer() {
var outerVar = "hi";
function inner(){
console.log(outerVar);
}
return inner();
}
var closureFn = outer();
closureFn(); // Output: hi
10. What is the difference between “let”, “const”, and “var”?
let
and const
were introduced in ES6 and have block scope. let
is reassignable, and const
is non-reassignable. var
is function-scoped and can be redeclared and reassigned throughout the function.
let x = 5;
x = 10;
console.log(x); // Output: 10
const y = 5;
y = 10; // Error: Assignment to constant variable
console.log(y);
var z = 5;
var z = 10;
console.log(z) // Output: 10
11. What is event delegation in JavaScript?
Event delegation is a technique where you attach a single event listener to a parent element, and that event listener handles events occurring on its child elements. It helps optimize performance and reduce memory consumption.
// HTML
<ul id = "list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
// JavaScript
document.getElementById("list").addEventListener("click", function(event){
if(event.target.nodeName === "LI") {
console.log(event.target.textContent);
}
});
12. What is implicit type coercion in JavaScript?
Implicit type coercion is a JavaScript concept that refers to the process of converting a value from one type to another. If you try to subtract a string to a number, JavaScript will implicitly coerce the string to a number before performing the subtraction operation.
For example, the following code will subtract the string "10"
to the number 5
. This is because JavaScript will implicitly coerce the string "10"
to a number before performing the subtraction operation.
var x = 5;
var y = "10";
console.log(x - y); // -5
13. Explain the concept of prototypes in JavaScript.
Prototypes are a mechanism used by JavaScript objects for inheritance. Every JavaScript object has a prototype, which provides properties and methods that can be accessed by that object.
function Person(name){
this.name = name;
}
Person.prototype.greet = function() {
console.log("Hello, " + this.name);
};
var person = new Person("Sayuj");
person.greet(); // Output: Hello, Sayuj
14. How can you clone an object in JavaScript?
There are multiple ways to clone an object in JavaScript. One common method is using the Object.assign()
method or the spread operator (...)
.
const obj1 = {name: "Sayuj", age: 25};
// Using Object.assign()
const obj2 = Object.assign({}, obj1);
// Using Spread operator
const obj3 = {...obj1};
console.log(obj2); // Output: {name: "Sayuj, age: 25}
console.log(obj3); // Output: {name: "Sayuj, age: 25}
15. What is the output of the following code?
console.log(6 + 4 + "7");
The output will be "107"
. The addition operation is performed from left to right, and when a string is encountered, it performs concatenation.