JavaScript Hoisting Explained part 2............................ The code demonstrates JavaScript’s let TDZ: console.log(x) before let x = 5 throws a ReferenceError. Though x is hoisted, it’s uninitialized and inaccessible until assignment. Unlike var, which defaults to undefined, let enforces strict initialization order. The TDZ ensures variables are used only after declaration, preventing bugs. After let x = 5, x is accessible and logs 5. This behavior promotes safer, more predictable code.
JavaScript Hoisting Explained with Let
More Relevant Posts
-
Ever felt confused about how reduce() actually works in JavaScript? 🤯 Where did the accumulator come from? Why does it sometimes start with 0? Why does the loop start from index 1 sometimes? The moment I finally understood it, everything clicked. If you've ever been confused about reduce(), my latest blog might help you. 👇 🔗 https://lnkd.in/g4hAcPyG #webdev #array-methods #reduce Hitesh Choudhary Piyush Garg Akash Kadlag Jay Kadlag
To view or add a comment, sign in
-
𝗚𝗲𝘁𝘁𝗶𝗻𝗴 𝗦𝘁𝗮𝗿𝘁𝗲𝗱 𝗪𝗶𝘁𝗵 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗧𝗲𝘀𝘁𝗶𝗻𝗴 𝗨𝘀𝗶𝗻𝗴 𝗝𝗲𝘀𝘁 You want to write JavaScript tests using Jest. To get started, you need to install Jest and babel-preset-es2015. Run this command: npm install --save-dev jest babel-preset-es2015 You need to configure your .babelrc file to use ESModules. It should look like this: - "presets": ["es2015"] Your package.json file should have a test script. It should look like this: - "scripts": {"test": "jest"} - "devDependencies": {"babel-preset-es2015": "^6.24.1", "jest": "^
To view or add a comment, sign in
-
Most JavaScript bugs are not because of bad code. They come from wrong assumptions. Example: You think: “This variable should be accessible here” JavaScript says: “Nope. Different scope.” You think: “This will run immediately” JavaScript says: “Wait for the event loop.” You think: “This function forgot the value” JavaScript says: “Closures already stored it.” 👉 JS is predictable. But only if you understand how it thinks. Stop fighting the language. Start understanding it. That’s when everything changes!
To view or add a comment, sign in
-
𝗧𝗶𝗽𝘀 𝗙𝗼𝗿 𝗖𝗹𝗲𝗮𝗻 𝗝𝗮𝗩𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗖𝗼𝗱𝗲 You want your JavaScript code to be clean and easy to read. Object destructuring helps you do this. It lets you extract object properties into variables in one line. Here's how it works: - Take properties from an object - Create variables with the same names You can set default values if a property does not exist. You can also rename variables if needed. Object destructuring is useful for: - Extracting nested values - Simplifying function parameters - Preventing undefined values - Making your code shorter and cleaner You can use object destructuring to: - Extract properties in one line - Set default values - Rename variables - Simplify function parameters Source: https://lnkd.in/gYEvMh5H
To view or add a comment, sign in
-
𝗧𝗵𝗲 𝗥𝗲𝗮𝗹 𝗥𝗲𝗮𝗸𝗼𝗻 𝗕𝗲𝗵𝗶𝗻𝗱 𝗔𝗿𝗿𝗼𝘄 𝗙𝘂𝗻𝗰𝗍𝗶𝗼𝗻𝘀 𝗶𝗻 𝗝𝗮 v𝗮𝗦𝗰𝗿𝗶𝗽𝘁 You've likely seen explanations of arrow functions that say they're just shorter functions. This explanation is incomplete. It focuses on syntax and ignores what changes under the hood. In JavaScript, functions define an execution context. A big part of that context is "this". With normal functions, "this" is dynamic. It's decided when the function is called. Arrow functions change this behavior. They capturethis from the surrounding scope at creation time. The real difference is howthis is handled: - Normal functions decidethis at call time - Arrow functions fix "this" at creation time Here's an example in Node.js: - Normal function: "this" is decided at call time, depends on who calls the function - Arrow function: "this" is taken from the surrounding scope, fixed at creation time Source: https://lnkd.in/gjH8gFMD
To view or add a comment, sign in
-
Just published a new blog on JavaScript fundamentals 🚀 Understanding the difference between Function Declarations and Function Expressions is crucial for writing better and more predictable code. In this blog, I’ve explained: ✅ Syntax differences ✅ Hoisting behavior ✅ When to use which ✅ Easy examples for clarity 🔗 Read here: https://lnkd.in/d7g3gAjE Feedback is always welcome! 😊 #JavaScript Chai Aur Code Hitesh Choudhary Piyush Garg Akash Kadlag Jay Kadlag Nikhil Rathore
To view or add a comment, sign in
-
If you misunderstand `𝐯𝐚𝐫`, `𝐥𝐞𝐭`, 𝐚𝐧𝐝 `𝐜𝐨𝐧𝐬𝐭`, your bugs will multiply quietly. Master these three, and your JavaScript becomes predictable. I like to explain them with simple mental models. `𝐯𝐚𝐫` is like a basic box. You can put something in it, replace it, and even access it outside the block where it was created. It’s flexible—but that flexibility often creates confusion due to function scope and re-declarations. `𝐥𝐞𝐭` is a box with a protective boundary. You can change what’s inside, but only within its block scope. Step outside that boundary, and it no longer exists. This makes your code safer and more controlled. `𝐜𝐨𝐧𝐬𝐭` is a locked cage. You cannot reassign it to something else. However, if it holds an object or array, the contents can still change—because the reference is locked, not the internal data. Understanding this difference prevents scope leaks, accidental overwrites, and unpredictable behavior. Here’s a practical rule: * Use `𝐜𝐨𝐧𝐬𝐭` by default * Use `𝐥𝐞𝐭` when reassignment is necessary * Avoid `𝐯𝐚𝐫` in modern JavaScript Clean code starts with disciplined variable declarations. When reviewing your code, are you choosing the right “box” intentionally—or out of habit? #JavaScript #FrontendDevelopment #WebEngineering #CleanCode #ProgrammingFundamentals #SoftwareEngineering #CodeQuality
To view or add a comment, sign in
-
-
Small JavaScript detail. Big source of bugs. Look at this code. let num1 = 8 let num2 = 2 console.log("Sum: " + num1 + num2); Many expect the output: Sum: 10 The real output: Sum: 82 Reason. JavaScript reads from left to right. The moment it hits a string, it converts the rest into string concatenation. Step by step execution. "Sum: " + 8 → "Sum: 8" "Sum: 8" + 2 → "Sum: 82" Fix the problem by calculating first. let total = num1 + num2; console.log("Sum: " + total); Output: Sum: 10 Takeaway. When combining numbers and strings in JavaScript, perform the calculation first. Then concatenate the result. Small detail. Cleaner code. Fewer bugs.
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development