function checkingThis(){
let z=10;
console.log(this.z);
}
checkingThis();
OUTPUT
undefined
I am not able to understand why
function checkingThis(){
let z=10;
console.log(this.z);
}
checkingThis();
OUTPUT
undefined
I am not able to understand why
See this: https://www.w3schools.com/js/js_this.asp
this has different values depending on where it is used:
this refers to the owner object .this refers to the global object .this refers to the global object .this is undefined .this refers to the element that received the event.call() , and apply() can refer this to any object .Since you are writing this.z, it tries to get the value of z defined in global object, which is undefined.
I tried defining z in global object as well,still the output came out to be undefine.
and bhaiya what is strict mode
Send the code you run. I tried it and it’s working.
let z =40; function checkingThis(){ console.log(this.z); } checkingThis();
Anirudh Bhaiya???
With strict mode, you can not, for example, use undeclared variables.
Try var z = 40 instead of let z = 40 This will work.
This is because let , unlike var , does not create a property on the global object. (https://stackoverflow.com/a/11444416/8183406)
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.