What are the different data types in JavaScript?

The different data types in JavaScript are:

1. Number: Any numerical value, such as 42 or 3.14159.

2. String: Any set of characters within quotation marks, such as “Hello World”.

3. Boolean: A true or false value, such as true or false.

4. Array: An ordered list of values, such as [1,2,3,4,5].

5. Object: A collection of key-value pairs, such as {name: “John”, age: 30}.

6. Null: An empty value, such as null.

7. Undefined: A value that has not been defined, such as undefined.

What is the difference between function and class declarations?

Function declarations are used to declare functions, which are blocks of code that can be called and reused multiple times. A function declaration consists of the function keyword, a name for the function, a list of parameters (which can be empty), and a body of code that will be executed when the function is called.

For example:

function addNumbers(a, b) {
return a + b;
}

Class declarations are used to declare classes, which are templates for creating objects. A class declaration consists of the class keyword, a name for the class, and a body of code that defines the properties and methods of the class.

For example:

class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name}`);
}
}

What is the difference between == and === operators?

== is the equality operator, which checks if the two values are equal or not. It doesn’t check the type of the two values. For example:

let a = 10;
let b = “10”;

console.log(a == b); // returns true

=== is the strict equality operator, which checks if the two values are equal and of the same type. For example:

let a = 10;
let b = “10”;

console.log(a === b); // returns false

What is the difference between JavaScript and ECMAScript?

JavaScript is a scripting language that was developed by Netscape. It is used to create interactive webpages and web applications. ECMAScript is a standardized version of JavaScript. It is a scripting language that is used to create webpages and web applications.

Example:

JavaScript:

let x = 10;
if (x > 5) {
alert(“x is greater than 5”);
}

ECMAScript:

let x = 10;
if (x > 5) {
console.log(“x is greater than 5”);
}

What are the advantages of using arrow functions? 9

1. Arrow functions are more concise than regular functions, making code more readable. For example:

// Regular Function
const add = function(a, b) {
return a + b;
};

// Arrow Function
const add = (a, b) => a + b;

2. Arrow functions do not create their own this context, which means they can access the this value of the enclosing context. For example:

// Regular Function
const myObj = {
name: ‘John’,
sayName: function() {
console.log(this.name);
}
};

// Arrow Function
const myObj = {
name: ‘John’,
sayName: () => console.log(this.name)
};

3. Arrow functions are automatically bound to the parent context, which means they can be used as callbacks or in methods like map, filter, and reduce. For example:

// Regular Function
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(function(number) {
return number * 2;
});

// Arrow Function
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(number => number * 2);

4. Arrow functions do not have their own arguments object, so they can’t be used as constructors. For example:

// Regular Function
function Person(name) {
this.name = name;
}

// Arrow Function
const Person = name => {
this.name = name;
};

5. Arrow functions are always anonymous, which means they can’t be used as object methods. For example:

// Regular Function
const person = {
name: ‘John’,
sayName: function() {
console.log(this.name);
}
};

// Arrow Function
const person = {
name: ‘John’,
sayName: () => console.log(this.name)
};

6. Arrow functions do not have a prototype property, so they can’t be used as constructors. For example:

// Regular Function
function Person(name) {
this.name = name;
}

Person.prototype.sayName = function() {
console.log(this.name);
};

// Arrow Function
const Person = name => {
this.name = name;
};

Person.prototype.sayName = () => console.log(this.name);

7. Arrow functions cannot be used as generators. For example:

// Regular Function
function* generator() {
yield 1;
yield 2;
yield 3;
}

// Arrow Function
const generator = () => {
yield 1;
yield 2;
yield 3;
};

8. Arrow functions are always anonymous, which makes them difficult to debug. For example:

// Regular Function
const add = function(a, b) {
return a + b;
};

// Arrow Function
const add = (a, b) => a + b;

9. Arrow functions cannot be used as async functions. For example:

// Regular Function
async function getData() {
const response = await fetch(‘https://example.com/data’);
return response.json();
}

// Arrow Function
const getData = async () => {
const response = await fetch(‘https://example.com/data’);
return response.json();
};

What is a callback function?

A callback function is a function that is passed as an argument to another function and is executed after some kind of event. It is essentially a way to make sure certain code does not execute until other code has already finished execution.

For example, let’s say you have a function called “doSomething” that takes two arguments, a number and a callback. The callback is a function that will be called when the doSomething function is finished running:

function doSomething(num, callback) {
// do some work
let result = num * 2;
// call the callback when finished
callback(result);
}

// call doSomething, passing in a number and our callback
doSomething(2, function(result) {
console.log(result); // 4
});

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

Var: Var is the oldest keyword for variable declaration in JavaScript. It is function scoped which means that it is visible inside the function it is declared in.
Example:
var x = 10;

Let: Let is the new keyword for variable declaration in JavaScript. It is block scoped which means that it is visible inside the block it is declared in.
Example:
let y = 20;

Const: Const is also a keyword for variable declaration in JavaScript. It is also block scoped and it cannot be reassigned or redeclared.
Example:
const z = 30;

What is the purpose of ‘use strict’ in JavaScript?

The purpose of “use strict” is to indicate that the code should be executed in “strict mode”. Strict mode is a way to opt in to a restricted variant of JavaScript. Strict mode eliminates some JavaScript silent errors by changing them to throw errors.

For example, in strict mode, assigning a value to a non-writable property will throw an error:

“use strict”;

var obj = {};
Object.defineProperty(obj, “x”, { value: 42, writable: false });

obj.x = 9; // throws an error in strict mode