Question-01: Difference between var let const?

The differences between var, let, and const variable declaration in JavaScript include: Variables declared with var and const are scoped to the immediate function body. Variables declared with the var keyword are hoisted. Hoisting means that the variable can be accessed in their enclosing scope even before they are declared. Variables declared with the let keyword are block-scoped, which means the variables will have scope to the immediate enclosing block. Let us now discuss, in detail, how these keywords can make a developers code more efficient and how to use each keyword to declare variables in JavaScript programs.

Question-02: Difference between arrow function and regular function?

An arrow function is also known as the fat arrow function. It is a new feature introduced in ES6 that is a more concise syntax for writing function expressions. Both regular JavaScript functions and arrow functions work in a similar manner but there are some differences between them.

1) Syntax: A programmer can get the same result as regular functions by writing a few lines of code using arrow functions. Curly brackets are not required if only one expression is present.

2) Arguments binding arguments object inside the regular functions contains the list of arguments. The arrow function, on the opposite, doesnt define arguments i.e. they do not have arguments binding. But you can easily access the arrow function arguments using a rest parameter ...args.

3) Use of this keyword Inside of a regular JavaScript function, this value is dynamic. The dynamic context means that the value of this depends on how the function is invoked. The behavior of this inside of an arrow, function differs considerably from the regular functions this behavior as an arrow function does not have its own “this” keyword.

4) Using a new keyword Regular functions are constructible and callable. They can be called using the new keyword. But, the arrow functions are only callable and not constructible, i.e., arrow functions can never be used as constructor functions.

Question-03: Difference between map, forEach, filter, find?

forEach: .forEach(), is used to execute the same code on every element in an array but does not change the array and it returns undefined. Example: In the example below we would use .forEach() to iterate over an array of food and log that we would want to eat each of them.

map: .map() executes the same code on every element in an array and returns a new array with the updated elements. Example: In the example below we would use .map to iterate over the elements of the cost array and divide each element by 10, then assign our new array containing the new cost to the variable newCost.

filter: .filter() checks every element in an array to see if it meets a certain criteria and returns a new array with the elements that return truthy for the criteria. Example: In the example below we would use .filter to return values that are less than 200.

find: .find() When you want to select a single element from an array.

Question-04: Why we use template string?

Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, for string interpolation with embedded expressions, and for special constructs called tagged templates. Template literals are sometimes informally called template strings, because they are used most commonly for string interpolation (to create strings by doing substitution of placeholders). However, a tagged template literal may not result in a string; it can be used with a custom tag function to perform whatever operations you want on the different parts of the template literal. Template literals are enclosed by backtick (`) characters instead of double or single quotes. Along with having normal strings, template literals can also contain other parts called placeholders, which are embedded expressions delimited by a dollar sign and curly braces: ${expression}. The strings and placeholders get passed to a function — either a default function, or a function you supply. The default function (when you don't supply your own) just performs string interpolation to do substitution of the placeholders and then concatenate the parts into a single string. To supply a function of your own, precede the template literal with a function name; the result is called a tagged template. In that case, the template literal is passed to your tag function, where you can then perform whatever operations you want on the different parts of the template literal. To escape a backtick in a template literal, put a backslash (\) before the backtick.