1.what is difference between var let and const in javascript
In JavaScript, users can declare a variable using 3 keywords that are
var, let, and const. In this article, we will see the differences
between the var, let, and const keywords. We will discuss the scope and
other required concepts about each keyword.
var keyword in JavaScript :
The var is the oldest keyword to declare a variable in JavaScript.
Scope : Global scoped or function scoped. The scope of the var keyword is
the global or function scope. It means variables defined outside the
function can be accessed globally, and variables defined inside a
particular function can be accessed within the function
let keyword in JavaScript : The let keyword is an improved version of the var keyword.
Scope: block scoped: The scope of a let variable is only block scoped. It can’t be accessible outside the particular block ({block}). Let’s see the below example.
const keyword in JavaScript : The const keyword has all the properties that are the same as the let keyword, except the user cannot update it.
Scope : block scoped: When users declare a const variable, they need to initialize it, otherwise, it returns an error. The user cannot update the const variable once it is declared.
2. what is difference between arrow function and normal function
Arrow function — also called fat arrow function — is a new feature introduced in ES6 that is a more concise syntax for a writing function expression.
Following are the main differences:
- Syntax
- Arguments binding
- Use of this keyword
- Using a new keyword
- No duplicate named parameters
A developer 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.If there’s only one argument, then the parentheses are not required either:
2) Arguments binding :
Arrow functions do not have arguments binding
3) ) Use of this keyword :
unlike Regular functions, arrow function does not have their own "this" keyword. The value of this inside an arrow function remains the same throughout the lifecycle of the function and is always bound to the value of this in the closest non-arrow parent function.
4) ) Using a new keyword :
Regular functions created using function declarations or expressions are constructible and callable. Regular functions are constructible; they can be called using the new keyword. However, the arrow functions are only callable and not constructible, i.e., arrow functions can never be used as constructor functions.
5) No duplicate named parameters :
Arrow functions can never have duplicate named parameters, whether in strict or non-strict mode. However, We can use duplicate named parameters for regular function in non-strict mode.
3. why we use template literals in javascript
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.