Understanding JavaScript's Global Object & this Keyword

🚀 Day 15:– JavaScript + DSA (Java) I learned: 🌐 Global Object & globalThis The Global Object is like a library containing all functions and variables accessible globally. In browsers, it’s window; in Node.js, it’s global. globalThis gives a uniform reference to the global object across environments. Inside functions, this behaves differently based on mode: Non-strict mode: this → global object Strict mode: this → undefined 🔹 Inside Objects & Methods this refers to the object that owns the method: const obj = { name: "Kaushal", greet: function() { console.log(this.name); // Kaushal } } obj.greet(); Arrow functions don’t have their own this, they inherit from the surrounding scope. 🔹 Constructors & Classes this points to the instance being created: class Person { constructor(name, age) { this.name = name; this.age = age; } } let a = new Person("Kaushal", 20); console.log(a); // Person {name: "Kaushal", age: 20} 💻 Also explored in Java: Generating all binary strings of a given length without consecutive 1s ✅ LeetCode #22-->Attempted balanced parentheses problem →still a work in progress 😅 #JavaScript #Java #LeetCode #CodingJourney #100DaysOfCode #LearningByDoing

To view or add a comment, sign in

Explore content categories