DEV Community

kanaga vimala
kanaga vimala

Posted on

JS>>>>Object,This Keyword and hoisting.

What is Object?

In JavaScript, an object is a complex data type that allows you to store collections of data and more complex entities. An object is made up of key-value pairs, where each key (also called a property name) is a string (or symbol), and each value can be any data typeโ€”such as a string, number, array, function, or even another object.

Basic Syntax

let person = {
name: "Alice",
age: 30,
isStudent: false
};

In the example above:

person is an object.

It has three properties: name, age, and isStudent.
Enter fullscreen mode Exit fullscreen mode

Accessing Object Properties

You can access or modify properties using dot notation or bracket notation:

console.log(person.name); // "Alice"
console.log(person["age"]); // 30

person.age = 31; // Update value
person.city = "New York"; // Add new property

Object Methods

An object can also contain functions, which are then called methods:

let person = {
name: "Alice",
greet: function() {
console.log("Hello, my name is " + this.name);
}
};

p**erson.greet(); // "Hello, my name is Alice"

Why Use Objects?**

Objects are useful for:

Grouping related data together.

Modeling real-world entities (like a car, user, order, etc.).

Passing structured data around in your programs.
Enter fullscreen mode Exit fullscreen mode

In JavaScript, the this keyword refers to the context in which a function is called. Its value depends on how and where the function is invoked.

๐Ÿง  Core Concept

this is a reference to the object that is executing the current function.
Enter fullscreen mode Exit fullscreen mode

**
๐Ÿ”น 1. In an Object Method**

const person = {
name: "Alice",
greet() {
console.log(this.name);
}
};

person.greet(); // "Alice" โ†’ this refers to the person object

๐Ÿ”น 2. In a Regular Function (not inside an object)

function show() {
console.log(this);
}

show(); // In non-strict mode: this is the global object (window in browser)
// In strict mode: this is undefined

๐Ÿ”น 3. In an Arrow Function

const person = {
name: "Alice",
greet: () => {
console.log(this.name);
}
};

person.greet(); // undefined โ†’ arrow functions donโ€™t bind their own this

โžก๏ธ Arrow functions inherit this from the surrounding lexical scope (where the function was defined).

๐Ÿ”น 4. In Event Handlers (DOM)

document.querySelector("button").addEventListener("click", function () {
console.log(this); // this refers to the button that was clicked
});

๐Ÿ”น 5. With bind(), call(), or apply()

You can manually set the value of this.

function greet() {
console.log(this.name);
}

const user = { name: "Bob" };

greet.call(user); // "Bob"
**
๐Ÿ“ Summary Table**
Context Value of this
Object method The object itself
Standalone function Global object (window or global)
Strict mode function undefined
Arrow function Lexical (outer) this
Event handler (non-arrow) Element that received the event
call / apply / bind Manually set

โšก What Is Hoisting?

Hoisting is JavaScript's behavior of moving declarations to the top of their scope (before code execution).

This applies to:

โœ… var variable declarations

โœ… Function declarations

โŒ NOT let and const (they are not hoisted in the same way)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Hoisting with var

console.log(a); // undefined
var a = 5;

Whatโ€™s actually happening:

var a; // hoisted declaration
console.log(a); // undefined
a = 5; // assignment happens later

โžก๏ธ var is hoisted and initialized with undefined.
๐Ÿ”น Hoisting with let and const

console.log(b); // โŒ ReferenceError
let b = 10;

let and const are hoisted but not initialized, so they stay in a "temporal dead zone" until the line where they are defined.

Accessing them before that results in an error.
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Hoisting with Function Declarations

greet(); // โœ… Works fine
function greet() {
console.log("Hello!");
}

Function declarations are fully hoisted โ€” both their name and body.
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Function Expressions (Not Hoisted Like Declarations)

sayHi(); // โŒ TypeError: sayHi is not a function

var sayHi = function () {
console.log("Hi!");
};

Only the variable (sayHi) is hoisted (as undefined), not the function assignment.
Enter fullscreen mode Exit fullscreen mode

โœ… Summary
Feature Hoisted? Initialized?
var โœ… Yes โœ… With undefined
let / const โœ… Yes (TDZ) โŒ No (TDZ error)
Function Declaration โœ… Yes โœ… Yes
Function Expression โœ… (as var) โŒ No

Top comments (0)