Lädt...


🔧 Demystifying JavaScript: Understanding Execution Contexts, Hoisting, and Type Conversion


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

JavaScript may seem simple on the surface, but under the hood, there’s a lot happening. Today, we’ll explore some essential concepts like execution contexts, hoisting, primitive vs. non-primitive data types, and type conversion. These are crucial to understand if you want to write better, bug-free code.

Image description

Global Execution Context and Lexical Environment
When you run a JavaScript file in the browser, the code gets executed line by line in the call stack. However, before any of your code runs, a global execution context is created. This context sets up the this and window objects. In Node.js, the equivalent of window is global, and if you compare the two, you'll find that window === global returns true.

Whenever you call a function, a new lexical environment is created. The global execution context is the first to be created, and all functions defined inside it can access its variables. This is how JavaScript’s scope chain works — you can access variables in the outer (global) scope from inside a function.

Hoisting: Variables and Functions
JavaScript has a mechanism called hoisting, where variables and functions are “moved” to the top of their scope during compilation. Here’s how it works:

Variables: Variables declared with var are partially hoisted, meaning you can reference them before they are initialized, but their value will be undefined until the line where they are initialized is reached.
Functions: Functions declared with the function declaration syntax are fully hoisted, meaning you can call the function even before its declaration in the code.
Example:

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

console.log(b); // Error: b is not defined
let b = 10;

hoistedFunction(); // Works!
function hoistedFunction() {
  console.log('This function is hoisted!');
}

notHoistedFunction(); // Error: notHoistedFunction is not a function
var notHoistedFunction = function() {
  console.log('This function is not hoisted!');
}

As you can see, let and const are not hoisted like var, and function expressions (like notHoistedFunction) are only defined at runtime.

Primitive vs. Non-Primitive Types
JavaScript has two types of data: primitive and non-primitive.

Primitive types include string, number, boolean, undefined, null, symbol, and bigint. These are immutable, meaning their values cannot be changed. For example:

let x = 'hello';
x[0] = 'H'; // This won’t change the string, it stays 'hello'

Non-primitive types are objects, arrays, and functions. These are mutable, and their values can be changed because they are passed by reference. For instance:

let obj1 = { name: 'John' };
let obj2 = obj1; // Both obj1 and obj2 now reference the same object
obj2.name = 'Doe';
console.log(obj1.name); // Outputs: Doe

To avoid modifying the original object, you can create a shallow copy using Object.assign() or the spread operator (...). For deep copies, which copy nested objects, use JSON.parse() and JSON.stringify().

Example Code Snippet: Shallow Copy vs Deep Copy

// Shallow copy example
let obj1 = { name: 'John', details: { age: 30 } };
let obj2 = { ...obj1 }; // Shallow copy
obj2.details.age = 40;
console.log(obj1.details.age); // Output: 40 (Shallow copy affects the original)

// Deep copy example
let obj3 = JSON.parse(JSON.stringify(obj1)); // Deep copy
obj3.details.age = 50;
console.log(obj1.details.age); // Output: 40 (Deep copy doesn’t affect the original)

Type Conversion and Comparison
JavaScript is a dynamically typed language, meaning you don’t have to specify variable types explicitly. However, this can sometimes lead to unexpected behavior, especially when using comparison operators.

Always prefer using triple equals (===) over double equals (==) to avoid type coercion. For example:

console.log(0 == '0'); // true (type coercion happens)
console.log(0 === '0'); // false (no type coercion)

For special cases, like comparing NaN, use Object.is() because NaN === NaN returns false.

console.log(NaN === NaN); // false
console.log(Object.is(NaN, NaN)); // true

JavaScript’s Runtime and Node.js
JavaScript runs on a single-threaded, synchronous runtime, which means it can only execute one task at a time. This might seem limiting, but JavaScript handles asynchronous tasks efficiently by using the Web API and callback queue. Here’s how it works:

When JavaScript encounters an async task (like setTimeout or an HTTP request), it sends the task to the Web API.
The call stack continues to execute the remaining code.
Once the async task is complete, it is added to the callback queue and will execute when the call stack is empty.
Node.js extends this runtime to the server-side, using the V8 engine and a non-blocking I/O system powered by libuv. Node.js introduced the idea of a single-threaded event loop that can handle multiple requests without blocking other operations.

By understanding how JavaScript handles execution contexts, hoisting, type conversion, and asynchronous tasks, you’ll be able to write cleaner and more efficient code. With JavaScript’s dynamic nature, tools like TypeScript can help you avoid common pitfalls by providing static type checks that make your code production-ready.

...

🔧 Demystifying JavaScript: Understanding Execution Contexts, Hoisting, and Type Conversion


📈 95.28 Punkte
🔧 Programmierung

🔧 What is Hoisting in JavaScript | Hoisting Functions, Variables and Classes


📈 47.34 Punkte
🔧 Programmierung

🔧 Demystifying Hoisting in JavaScript😵


📈 39.82 Punkte
🔧 Programmierung

🔧 Understanding JavaScript keyword var, let, const and hoisting.


📈 34.04 Punkte
🔧 Programmierung

🔧 Understanding Variable Access in JavaScript: Scope, Hoisting, and Closures


📈 34.04 Punkte
🔧 Programmierung

🔧 UNDERSTANDING HOISTING IN JAVASCRIPT


📈 32.74 Punkte
🔧 Programmierung

🔧 Understanding Hoisting in JavaScript


📈 32.74 Punkte
🔧 Programmierung

🔧 Understanding Hoisting in JavaScript


📈 32.74 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Hoisting


📈 32.74 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Hoisting: A Simple Guide


📈 32.74 Punkte
🔧 Programmierung

🔧 Understanding Hoisting in JavaScript: A Comprehensive Guide


📈 32.74 Punkte
🔧 Programmierung

🔧 JavaScript Execution Context & Hoisting


📈 30.47 Punkte
🔧 Programmierung

🔧 Understanding Switch Case and Functions and What is Hoisting


📈 29.68 Punkte
🔧 Programmierung

🔧 Js | Type Conversion & Type Coercion |


📈 29 Punkte
🔧 Programmierung

🔧 Understanding Reactive Contexts in Angular 18


📈 28.48 Punkte
🔧 Programmierung

📰 Assessing OpenAI’s o1 LLM in Medicine: Understanding Enhanced Reasoning in Clinical Contexts


📈 28.48 Punkte
🔧 AI Nachrichten

🔧 Hoisting in JavaScript with let and const – and How it Differs from var


📈 28.47 Punkte
🔧 Programmierung

🔧 JavaScript Type Conversion and Coercion


📈 28.03 Punkte
🔧 Programmierung

🔧 Common JavaScript Type Conversion Mistakes and How to Fix Them


📈 28.03 Punkte
🔧 Programmierung

🔧 Understanding Type Conversion in Programming: Key Points You Need to Know


📈 27.94 Punkte
🔧 Programmierung

🔧 Day 3:From Strings to Numbers: Demystifying JavaScript Type Conversions


📈 27.59 Punkte
🔧 Programmierung

🔧 JavaScript Scope and Hoisting


📈 27.16 Punkte
🔧 Programmierung

🔧 Scope, Hoisting and Closures in Javascript


📈 27.16 Punkte
🔧 Programmierung

🔧 Scope, Closures, and Hoisting in JavaScript – Explained with Code Examples


📈 27.16 Punkte
🔧 Programmierung

🔧 Hoisting, Lexical Scope, and Temporal Dead Zone in JavaScript


📈 27.16 Punkte
🔧 Programmierung

🔧 Unraveling JavaScript: A Deep Dive into Hoisting, Temporal Dead Zone, and Variable States


📈 27.16 Punkte
🔧 Programmierung

🔧 Temporal Dead Zone (TDZ) and Hoisting in JavaScript:


📈 27.16 Punkte
🔧 Programmierung

🔧 Scopes and Hoisting in JavaScript - Comprehensively Explained


📈 27.16 Punkte
🔧 Programmierung

🔧 A NEW Quiz is out: JavaScript Hoisting and Scope Quiz


📈 27.16 Punkte
🔧 Programmierung

🔧 A Guide to Mastery in Type Conversion on JavaScript


📈 26.72 Punkte
🔧 Programmierung

🔧 JavaScript data type conversion


📈 26.72 Punkte
🔧 Programmierung

🔧 Type Conversion in JavaScript Is Confusing


📈 26.72 Punkte
🔧 Programmierung

🔧 Type Conversion in JavaScript Is Confusing


📈 26.72 Punkte
🔧 Programmierung

🔧 Type Conversion in Javascript


📈 26.72 Punkte
🔧 Programmierung

matomo