1- What is difference between var and let ?
Ans: i) var is in Javascript since begining and let is introduced in ES2015/ ES6
ii) let has block scope and var has function scope
e.g:
function myfun() {
if (true) {
var a = 2;
let b = 1;
}
console.log(a); //2
console.log(b); // b is not defined
}
iii) var get hoisted to it's top of it's function
e.g:
function myfun() {
if (true) {
console.log(a); // undefined
console.log(b); // b is not defined
var a = 2;
let b = 1;
}
}
2- what is difference between let and const
ans: const will not let you to reassign a value but you can modify it
let will let you do the reassign value as well as modification to value
3- what is difference between undefied and null
ans : Both represent empty value but when we define variable and not assign value to it then javascript put it is a undefined and
null we do it by assigning it manually we can also assign undefined but which is not a good idea
typeof(undefined) = => undefined
typeof(null) ==> object
4- What is prototype inheritance in Javascript ?
ans : Every object of javscript has prototype property by default
This is why you can use a method like sort() on an array instance.
prototypical inheritance refers to the ability to access object properties from another object. We use a JavaScript prototype to add new properties and methods to an existing object constructor. We can then essentially tell our JS code to inherit properties from a prototype
5- What Closure in javascript ?
ans: A closure is a function having access to the parent scope, even after the parent function has closed
(we can use it for counter type of method)
let Obj = function () {
let i = 0;
return {
setI(k) {
i = k;
}
getI() {
return i;
}
}
}
let x = Obj();
x.setI(2);
console.log(x.getI); // 2
No comments:
Post a Comment