const in javascript It prints 2. Even though we used const. Most developers think const means unchangeable. So when an object’s property updates successfully, it feels wrong. Like JavaScript ignored its own rule. But it didn’t. const prevents reassignment. It doesn’t freeze the value inside. The variable is locked. The object it points to isn’t. This subtle distinction causes real confusion in frontend development, interviews, and production code. Understanding reference vs value is one of those JavaScript quirks that separates confidence from correctness. If you're learning clean JavaScript, debugging tricky behavior, or preparing for JavaScript interview concepts, this is foundational. Follow CodeBreakDev for code that looks right… but isn’t.
More Relevant Posts
-
🚨 Ever seen JavaScript code that looks like a staircase? 💡 In JavaScript, a callback is a function that runs after a task finishes. For example, after fetching data from an API. Sounds easy… until multiple tasks depend on each other. Then the code starts looking like this: ➡️ Get users ➡️ Then get their posts ➡️ Then get comments ➡️ Then get the comment author Every step waits for the previous one. And suddenly code becomes a deep pyramid of nested functions, often called the “Pyramid of Doom” or "Sideways Triangle." ⚠️ Why developers avoid it: 🔴 Hard to read 🔴 Hard to debug 🔴 Hard to maintain ✨ Modern JavaScript solves this with: ✅ Promises ✅ async / await Both make asynchronous code cleaner and easier to understand. What JavaScript concept confused you the most when you started learning? 👇 Let’s discuss. #JavaScript #WebDevelopment #CodingJourney #AsyncProgramming #LearnInPublic
To view or add a comment, sign in
-
-
𝗨𝗻𝗱𝗲𝗿𝗦𝘁𝗮𝗻𝗱𝗶𝗻𝗴 𝗝𝗮 v𝗮𝗦𝗰𝗿𝗶𝗽𝘁'𝘀 𝗘𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻 𝗖𝗼𝗻𝘁𝗲𝘅𝘁𝘀 JavaScript is a nuanced language. Its execution contexts are critical to understand. You need to know how code is executed to leverage advanced features. An execution context is an environment that holds the code that is currently executing. It manages variable scoping and function execution. Every time a function is invoked or a script runs, a new execution context is created. - Variable Environment: Stores variable bindings. - Scope Chain: Determines the hierarchy of variable access. - this Value: A reference to the object from which a function was called. - Lexical Environment: Encompasses the variables and functions available. JavaScript maintains a call stack to track execution contexts. The context at the top of the stack is the context currently executing. You can create a new execution context with a function call. The context is pushed to the stack when a function call is made and popped when the function returns. Understanding execution contexts is key to mastering JavaScript. It helps you write secure, efficient, and maintainable code. You can use tools to monitor variable bindings and detect memory leaks. Source: https://lnkd.in/gFuWcd-E
To view or add a comment, sign in
-
𝗨𝗻𝗱𝗲𝗿𝗦𝘁𝗮𝗻𝗱𝗶𝗻𝗴 𝗝𝗮 v𝗮𝗦𝗰𝗿𝗶𝗽𝘁'𝘀 𝗘𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻 𝗖𝗼𝗻𝘁𝗲𝘅𝘁𝘀 JavaScript is a nuanced language. Its execution contexts are critical to understand. You need to know how code is executed to leverage advanced features. An execution context is an environment that holds the code that is currently executing. It manages variable scoping and function execution. Every time a function is invoked or a script runs, a new execution context is created. - Variable Environment: Stores variable bindings. - Scope Chain: Determines the hierarchy of variable access. - this Value: A reference to the object from which a function was called. - Lexical Environment: Encompasses the variables and functions available. JavaScript maintains a call stack to track execution contexts. The context at the top of the stack is the context currently executing. You can create a new execution context with a function call. The context is pushed to the stack when a function call is made and popped when the function returns. Understanding execution contexts is key to mastering JavaScript. It helps you write secure, efficient, and maintainable code. You can use tools to monitor variable bindings and detect memory leaks. Source: https://lnkd.in/gFuWcd-E
To view or add a comment, sign in
-
💡 JavaScript Tip: Start using the .at(-1) today! If you're still accessing the last element of an array like this: arr[arr.length - 1] There’s a cleaner and more readable way 👇 arr.at(-1) 🔹 Why use .at()? ✅ Cleaner syntax ✅ Easier to read ✅ Supports negative indexing 🔹 Examples const nums = [10, 20, 30, 40]; nums.at(0); // 10 nums.at(2); // 30 nums.at(-1); // 40 (last element) nums.at(-2); // 30 🔹 When to use it? Accessing last or second-last elements Writing cleaner, more modern JavaScript Avoiding repetitive .length calculations ⚠️ Note .at() works in modern JavaScript (ES2022+), so ensure your environment supports it. Small improvements like this can make your code more readable and elegant ✨ Are you using .at() already? #JavaScript #CleanCode #WebDevelopment #FrontendDevelopment #ProgrammingTips #DevCommunity #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Mastering "this" in JavaScript (Without Confusion) One of the most misunderstood concepts in JavaScript is how this actually works. Here’s the simplest mental model I use 👇 🧠 Key Rules: • Normal function → this depends on how it's called • Arrow function → this is inherited from its surrounding scope • Method call → this = object before the dot • Plain function call → this = undefined (strict mode) • Arrow inside method → inherits method’s this • Arrow as method → usually incorrect (binds global scope) 💡 Key Insight: this is not about where you define a function — it’s about how JavaScript executes it. Understanding this removes a lot of confusion around callbacks, objects, and async code. 🧩 Optimizing Problem Solving (Array Patterns) Focused on improving time & space complexity: • Maximum Subarray → Kadane’s Algorithm (O(n)) • Product of Array Except Self → Prefix/Suffix (O(n), no division) • Majority Element → Boyer–Moore Voting (O(1) space) Instead of stopping at brute force, pushed toward optimal solutions. 💡 Takeaway: Every problem has a solution. Good developers find it. Great developers optimize it. Sharpening fundamentals to write cleaner, more efficient code. 💪 #JavaScript #DSA #ProblemSolving #FrontendDeveloper #MERNStack #SoftwareEngineering
To view or add a comment, sign in
-
-
𝐋𝐞𝐭'𝐬 𝐞𝐱𝐩𝐥𝐨𝐫𝐞 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐚 𝐛𝐢𝐭😉 This question caught my attention with how simple it is, based on inference from my experience with JavaScript, and I thought to explain 𝗜𝘀 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗮 𝗱𝘆𝗻𝗮𝗺𝗶𝗰𝗮𝗹𝗹𝘆 𝘁𝘆𝗽𝗲𝗱 𝗼𝗿 𝘀𝘁𝗮𝘁𝗶𝗰𝗮𝗹𝗹𝘆 𝘁𝘆𝗽𝗲𝗱 𝗹𝗮𝗻𝗴𝘂𝗮𝗴𝗲? 𝐓𝐡𝐞 𝐚𝐧𝐬𝐰𝐞𝐫: JavaScript is dynamically typed. In dynamically typed languages, all type checks are performed at runtime, that is, when the program is executing. So this means you can assign anything to the variable and it will work. This is because types are associated with values, not variables. This flexibility is one of the reasons JavaScript became so popular. It allows developers to move quickly and write readable code. But it also introduces trade-offs. (Nothing really is for free😔) Because types are checked only at execution, certain mistakes only appear when the code runs. Some of which include: ☑ Unexpected type coercion ☑ Runtime errors ☑ Harder-to-maintain large codebases This is one of the reasons TypeScript gained popularity. Unlike JavaScript, TypeScript, a statically typed language, insists on all checks being performed during compile/build run before we execute our program. This allows developers to catch many type-related errors before the code runs. In the end, understanding JavaScript’s dynamic nature helps you: ☑ Write safer code ☑ Avoid subtle bugs ☑ Appreciate when tools like TypeScript are helpful #JavaScript #TypeScript #WebDevelopment #SoftwareEngineering #FrontendDevelopment
To view or add a comment, sign in
-
📣 𝗡𝗲𝘅𝘁 𝗕𝗹𝗼𝗴 𝗶𝘀 𝗛𝗲𝗿𝗲! ⤵️ Arrow Functions in JavaScript — A Simpler Way to Write Functions ⚡🧠 Regular functions work fine. But when logic becomes small and frequent, typing all that syntax starts feeling heavy. This blog explains arrow functions in a clear, beginner-friendly way — focusing on syntax, mental models, and real usage. 🔗 𝗥𝗲𝗮𝗱 𝗵𝗲𝗿𝗲: https://lnkd.in/gMBAZxX5 𝗧𝗼𝗽𝗶𝗰𝘀 𝗰𝗼𝘃𝗲𝗿𝗲𝗱 ✍🏻: ⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ ⇢ What arrow functions actually are ⇢ Converting normal functions to arrow step by step ⇢ Basic syntax and parameter rules ⇢ Implicit return — the real magic ⇢ When implicit return does NOT work ⇢ Using arrow functions in map(), filter(), and callbacks ⇢ Common beginner mistakes and how to fix them ⇢ When to use arrow functions vs regular functions ⇢ Practical examples for faster understanding 💬 If functions still feel verbose or repetitive, this article helps you write cleaner and more modern JavaScript. #ChaiAurCode #JavaScript #ArrowFunctions #WebDevelopment #ProgrammingBasics #Beginners #LearningInPublic #100DaysOfCoding
To view or add a comment, sign in
-
-
🚀 JavaScript Spread vs Rest Operator — Same Syntax, Opposite Purpose! Understanding the difference between Spread (...) and Rest (...) operators is essential for writing clean and modern JavaScript code. Although both use the same ... syntax, they perform completely opposite tasks. 🔹 Spread Operator (...) Expands values outward • Breaks an iterable into individual elements • Useful for merging arrays or cloning objects • Common in function calls and object/array literals Example: const a = [1,2,3]; const b = [4,5,6]; const merged = [...a, ...b]; // [1,2,3,4,5,6] 🔹 Rest Operator (...) Collects values into one place • Gathers multiple arguments into an array • Used in function parameters and destructuring • Must always be the last parameter Example: function sum(...nums){ return nums.reduce((a,b) => a + b, 0); } 📌 Key Rule to Remember Spread → Expands values Rest → Collects values Small JavaScript concepts like this make a big difference in writing cleaner and more efficient code. 💬 What other JavaScript concepts should I explain next? If this helped you: 👍 Like | 💬 Comment | 🔁 Repost #JavaScript #WebDevelopment #FrontendDevelopment #SoftwareDevelopment #Programming #Coding #Developer #JavaScriptTips #TechLearning #FullStackDeveloper #DevCommunity #LearnToCode
To view or add a comment, sign in
-
-
JavaScript's parser has a split personality and it will silently give you a completely different result based on where you place a {}: [] + [] → "" [] + {} → "[object Object]" {} + [] → 0 Same operands. Swapped order. Completely different result and it's not even a number anymore?? Here's what's actually happening 👇 When {} comes first, JavaScript doesn't see an object literal. It sees an empty block statement like an empty if body. So it reads the line as: {} // ← empty block (ignored) +[] // ← unary + operator on an array Unary + forces type conversion → +[] converts [] to "" → "" to 0. Result: 0 Flip it, and {} is now in expression position so JS treats it as an object. Then string concatenation kicks in. Result: "[object Object]" Same characters. Different position. Two completely different mental models activated by the parser. This is why JavaScript's implicit type coercion + context-sensitive parsing is a beautiful nightmare. The fix? Always be explicit: Number([]) // 0 String({}) // "[object Object]" Never let JS guess. It has terrible taste. 😅 Save this for your next code review, guaranteed to start a conversation. JavaScript Mastery w3schools.com
To view or add a comment, sign in
-
-
Most developers use JavaScript… but don’t truly understand it. Here’s the truth: If you don’t understand: Hoisting Scope Execution Context You’re not writing JavaScript… You’re just guessing Example: Why does this work? console.log(a); var a = 10; And this breaks? console.log(b); let b = 10; The answer lies in how JavaScript executes code internally. Behind every line of JS: Execution Context is created Memory is allocated (Hoisting) Scope chain is formed Then code runs Once you understand this, everything clicks: Closures Async JS Debugging complex bugs This is not “advanced”… This is fundamentals most people skip. Start mastering the engine, not just the syntax. Follow Royal Decode for more real dev insights #JavaScript #CodingJourney #FrontendDeveloper #ProgrammingLife #DevTips #LearnJavaScript #RoyalResearch
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