JS interview Questions -Piyush Garg
// console.log("Value of Age is",age);
// var age=2
// console.log("Value of Age is",age);
/**
* undefined
* 2
*
* Because of Hoisting
* Global execution context
* Memory phase --> js will traverse whole code and it will load that in memory phase
* code phase
*
*/
// age =100;
// console.log("Value of age is",age);
// let age =30;
/**
* Throw reference Error
* trying to acess age before initialization
*
* let and const are hoisted differently
* they are hoisted inside
Temporal Dead Zone
*the area of a block where a variable is inaccessible until the moment the computer completely initializes it with a value.
*/
// myFun();
// var myFun = function(){
// console.log('First')
// }
// myFun();
// function myFun(){
// console.log('Second');
// }
// myFun();
/**
* Second
*
* First
*
* First
*
*
*
* whenever we run a js code it will create
* a global execution context
*
* in the memory phase first it will
* define myFunc as a variable
* then update it with a function
* when code execution phase starts
* it will invoke the Second function
* now function def gets updated with the first
* one
*
*
*
*/
// var variable =10;
// (()=>{
// console.log(variable);
// variable=20;
// console.log(variable);
// })(); //iife
// console.log(variable);
// var variable=30;
/**
* variable : undefined/10/20/20
*
*
*/
// foo =30;
// console.log("FOO",foo);
// var foo=100;
// console.log("FOO",foo)
/*
30
100
*/
// variable=10;
// (()=>{
// foo=100;
// console.log(variable);//10
// var foo=100;
// variable=20;
// console.log(variable)//20
// })();
// console.log(foo); //foo is not defined
// console.log(variable);
// var variable =30;
// for(var i=0; i<10;i++){
// setTimeout(function() {
// console.log(i)
// }, 0);
// }
// for(let i=0; i<10;i++){
// setTimeout(function() {
// console.log(i)
// }, 0);
// }
/**
since var has a gloal context
var i=1...10;
setTimeout(i)
setTimeout(i)
setTimeout(i)
setTimeout(i)
.....
what if we make it let
since let scope is local
it creates new cotext each and every
time
*/
// var fullName ="Ankit Tyagi"
// var obj ={
// fullName:'Hacked Full Name',
// prop:{
// fullName : 'Inside Prop',
// getFullName: function(){
// return this.fullName;
// }
// },
// getFullName: function(){
// return this.fullName;
// },
// getFullNameV2:()=> this.fullName,
// getFullNameV3:(function(){
// return this.fullName;
// })(),
// };
// console.log(obj.prop.getFullName());
// //Inside Prop
// console.log(obj.getFullName());
// //Hacked Full Name
// console.log(obj.getFullNameV2());
// //undefined --> it is poiniting to window/global object
// //console.log(obj.getFullNameV3());
// //ERROR -->because it is a variable not a function , we are assigning it value after function execution.
// const ankit={
// name:'Ankit Tyagi',
// sayName: function(){
// console.log(this.name);
// }
// }
// const john={
// name: "Jhon Doe",
// sayName: function(){
// console.log(this.name);
// }
// }
// john.sayName();
// john.sayName.call(ankit);
// /**
// function borrowing
// */
// const ankit={
// name:'Ankit Tyagi',
// sayName: function(){
// console.log(this.name);
// }
// }
// setTimeout(ankit.sayName.bind(ankit),3*1000);
// setTimeout(()=>ankit.sayName(),3*1000)
// const obj={
// height:30
// }
// console.log(obj.height);
// delete obj.height;
// console.log(obj.height);
// const obj=Object.create({
// height:30
// });
// console.log(obj.height);
// delete obj.height;
// console.log(obj.height);
Comments
Post a Comment