Use of this keyword in JavaScript ?
In : BSc IT Subject : Web DesigningThe this keyword in JavaScript is a special reference that refers to the context in which a function is executed — or more simply, it tells you what object the function belongs to when it runs .
1. In Global Scope
console.log(this); // In browser: refers to `window` object
Outside of any function or object, this points to the global object (window in browsers, global in Node.js).
2. In a Function (Simple Call)
function showThis() {
console.log(this);
}
showThis(); // Still refers to the global object (`window`)
In non-strict mode, this still refers to the global object.
In strict mode ("use strict";), this will be undefined.