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

What is the difference between a function expression and a function declaration?

A function expression is an expression that defines a function. It is usually written as a variable assignment, where a function is assigned to a variable. For example:

const myFunction = function() {
// code here
};

A function declaration is a statement that declares a function. It is written with the function keyword followed by the function name, parameters, and code. For example:

function myFunction() {
// code here
}

What is the purpose of using closures in JavaScript?

Closures are used to create private variables and functions in JavaScript. A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function’s variables—a scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

Example:

function outerFunction(x) {
let innerVariable = 3;

function innerFunction(y) {
return x + y + innerVariable;
}

return innerFunction;
}

let innerFunc = outerFunction(5);

console.log(innerFunc(2)); // 10

What are the different scopes of a variable in JavaScript?

The different scopes of a variable in JavaScript are:

1. Global Scope: A global scope variable is declared outside of any function and can be accessed from any part of the program.

Example:
var x = 10;

2. Local Scope: A local scope variable is declared inside a function and can only be accessed within that function.

Example:
function myFunction() {
var y = 20;
}

3. Block Scope: A block scope variable is declared inside a block statement and can only be accessed within that block statement.

Example:
if (true) {
let z = 30;
}

What are the advantages of using JavaScript?

1. Easy to learn and use: JavaScript is relatively easy to learn and use compared to other programming languages, and it is also widely used, so it is easy to find resources and tutorials for help. For example, if you wanted to make a simple website with a few interactive features, you could easily learn the basics of JavaScript and use it to make your website come to life.

2. Cross-platform compatibility: JavaScript can run on multiple platforms, including web browsers, servers, and mobile devices. This means that you can write code once and it will work on any platform. For example, you could write a JavaScript program that runs on a web browser, and then easily port it to a mobile device with minimal changes.

3. Rich interfaces: JavaScript is used to create interactive web interfaces that make websites more user-friendly and engaging. For example, you could use JavaScript to create drop-down menus, sliders, and other interactive elements that make navigating a website easier.

4. Increased speed: JavaScript can be used to reduce the amount of time it takes for a website to load. For example, you could use JavaScript to pre-load images and other content, so that when a user visits a page, the content is already loaded and ready to go. This can significantly increase the speed of a website.

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

== is the comparison operator, also known as the “equal to” operator. It checks if the two values on either side of the operator are equal. For example:

let x = 5;
let y = 5;
x == y // returns true

=== is the strict comparison operator, also known as the “equal value and equal type” operator. It checks if the two values on either side of the operator are equal and of the same type. For example:

let x = 5;
let y = ‘5’;
x === y // returns false