How to use this in function with JavaScript

2 Answers

0 votes
function f() {
  return this;
}
 
console.log(f());
 
console.log(f() === window);
 
console.log(f() === globalThis);
 
 
 
 
/*
run:
 
: [object Window]
true
true
 
*/

 



answered Nov 20, 2020 by avibootz
0 votes
function f() {
  'use strict'
  return this;
}

console.log(f());

console.log(f() === window);

console.log(f() === globalThis);

console.log(f() === undefined);




/*
run:

undefined
false
false
true

*/

 



answered Nov 20, 2020 by avibootz

Related questions

1 answer 143 views
1 answer 155 views
2 answers 192 views
1 answer 148 views
1 answer 132 views
...