Today I learned something that completely changed how I think about JavaScript function arguments. For a long time, I believed: Primitive values → Pass by value Objects/Arrays → Pass by reference But here’s the interesting part: In JavaScript, everything is passed by value. Yes — even objects. When we pass an object into a function, we are not passing the actual object. We pass a copy of the reference (memory address). So technically: Primitive → Copy of value Non-primitive → Copy of reference That small difference explains why: -Changing a primitive inside a function doesn’t affect the original. -Modifying properties of an object does affect the original. -Reassigning the object itself does NOT affect the original. Understanding this cleared up so many confusing bugs for me. Golden Rule: If you mutate → original changes If you reassign → original does not change Sometimes growth in coding isn’t about learning something new —it’s about correcting what you thought you already knew. #JavaScript #WebDevelopment #Learning #FrontendDeveloper #Functions
JavaScript Function Arguments: Pass by Value or Reference?
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
-
-
🚨 JavaScript once told me 1 + "1" equals 11. And surprisingly… it wasn’t wrong. That’s Type Coercion - one of the most misunderstood parts of JavaScript. Type coercion happens when JavaScript automatically converts values from one type to another. This is why JavaScript sometimes feels unpredictable. But here’s the truth: It’s not random. It’s rule-based. Almost every strange behavior comes from how operators trigger conversions: • Math operations treat values as numbers • The + operator switches to string concatenation when text is involved • Conditionals convert values into true or false when making decisions Once you understand that operators decide how values are interpreted, JavaScript starts feeling far more logical. Type coercion isn’t a flaw in the language. It exists because the web deals with messy real-world input - forms, APIs, and user data that don’t always arrive in the expected type. The real skill isn’t avoiding type coercion. It’s understanding when JavaScript applies its rules - and why.. #JavaScript #WebDevelopment #Programming #SoftwareEngineering #FrontendDevelopment
To view or add a comment, sign in
-
-
Day 1 of 30 days of javascript challenge. problem-2667 Problem - Write a function createHelloWorld that returns another function, that returns "Hello World" As this is my first code, I revised my notes on javascript scope. ☑️ Function scope - Any variables declared inside a function body cannot be accessed outside the function body, but global variables can be used inside function body ☑️ Block scope - Any variable declared inside { } cannot be used outside the { } block, although it supports only let and const keyword, var can be used ☑️ Lexical scope - A variable declared outside a function can be accessed inside another function defined after the variable declaration. (The opposite is not true ) This problem uses the concept of closures and higher order functions. Please feel free to discuss where can I improve the code or if you have a different perspective, comment below your views. #javascript #coding #development #motivation #goals #leetcode #webdevelopment
To view or add a comment, sign in
-
-
Just published a new blog on this, call(), apply(), and bind() in JavaScript. These concepts can feel confusing at first, especially when this behaves differently depending on how a function is called. In this blog, I tried to break things down in a simple way: • What this actually means in JavaScript • How call() and apply() let you control the value of this • Why bind() creates a new function with a fixed context • The practical difference between all three If you are learning JavaScript and these concepts ever felt confusing, this might help 👇 https://lnkd.in/guju6-cn Thanks to Hitesh Choudhary Chai Aur Code Piyush Garg Nikhil Rathore Akash Kadlag Jay Kadlag Suraj Kumar Jha for guidance! #javascript #webdevelopment #frontenddevelopment #coding #programming #learninpublic #100daysofcode #webdev #softwaredevelopment
To view or add a comment, sign in
-
Today I explored one of the most confusing but fascinating concepts in JavaScript — The Event Loop. JavaScript is single-threaded, but it still handles asynchronous tasks like API calls, timers, and promises smoothly. The magic behind this is the Event Loop. Here’s the simple flow: 1️⃣ Call Stack – Executes synchronous code 2️⃣ Web APIs – Handles async tasks (setTimeout, fetch, DOM events) 3️⃣ Callback Queue / Microtask Queue – Stores callbacks waiting to execute 4️⃣ Event Loop – Moves tasks to the call stack when it’s empty 💡 Example: console.log("Start"); setTimeout(() => { console.log("Timeout"); }, 0); Promise.resolve().then(() => { console.log("Promise"); }); console.log("End"); 🧠 Output: Start End Promise Timeout Why? Because Promises go to the Microtask Queue which runs before the Callback Queue. ✨ Learning this helped me finally understand how JavaScript manages async behavior without multi-threading. Tomorrow I plan to explore another interesting JavaScript concept! Devendra Dhote Ritik Rajput #javascript #webdevelopment #frontenddeveloper #100DaysOfCode #learninginpublic #codingjourney #sheryianscodingschool
To view or add a comment, sign in
-
🚀 Arrays are NOT real arrays in JavaScript. That was one of the most interesting things I learned today while studying Arrays in JavaScript. At first, arrays seem simple — just a collection of elements. But under the hood, JavaScript arrays are actually objects with special behavior, not fixed-size contiguous memory structures like in languages such as C++. What I learned about Arrays: • Arrays in JavaScript are mutable • They can store multiple data types • They are dynamic in size Key operations I practiced: • Adding/removing elements → push(), pop(), shift(), unshift() • Looping → classic for loop and modern iteration methods • Searching → indexOf(), lastIndexOf(), includes() • Manipulation → slice(), splice() • Conversion → join() (array → string) • Spread operator → modern and powerful way to copy/merge arrays One important insight: The sort() method can behave unexpectedly with numbers because it sorts values as strings by default. Example: [10, 2, 5].sort() // Output -> [10, 2, 5] To sort numbers correctly, we need a comparison function. To summarize everything, I created a detailed carousel Notes (Notes given by Rohit Negi). Course Instructor: Rohit Negi | Youtube Channel: Coder Army #JavaScript #WebDevelopment #LearningJourney #BuildInPublic #FrontendDevelopment #Fullstackdevelopment #Coding
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
-
-
Ever clicked a button and noticed that the parent element’s event also runs? That happens because of event bubbling in JavaScript. When an event occurs, it travels up the DOM tree: Button → Parent → Document Sometimes this behavior causes unexpected results in your application. That’s where stopPropagation() helps. It stops the event from moving to parent elements. Example: element.addEventListener("click", (e) => { e.stopPropagation(); }); Now the event will stay on the current element only. Quick reminder: preventDefault() → stops browser default action stopPropagation() → stops event bubbling Understanding small concepts like this helps you write cleaner and more predictable JavaScript code. Follow for more JavaScript concepts explained visually. #javascript #webdevelopment #frontenddeveloper #coding #softwareengineering
To view or add a comment, sign in
-
-
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.
To view or add a comment, sign in
-
🚀 JavaScript Magic: Why "Undefined" is actually a Feature, not a Bug! I just had a "Wow" moment diving into the JavaScript Execution Context, and it changed how I look at my code. Ever wondered why you can console.log a variable before you even declare it, and JavaScript doesn't lose its mind? 🤯 🧠 The Secret: Two-Phase Execution When your code runs, JavaScript doesn't just start at line 1. It takes two passes: 1.Memory Creation Phase: JS scans your code and allocates space for all variables and functions. 2. Execution Phase: It runs the code line-by-line. ⚡ The var Behavior (Hoisting) If you use var, JavaScript initializes it as undefined during the memory phase. Result: You can log it early. No error, just a quiet undefined. It’s like the variable is there, but its "suit" hasn't arrived yet. 🛑 The let & const Twist (TDZ) Try the same thing with let or const, and the engine throws a ReferenceError. Why? The Temporal Dead Zone (TDZ). While let and const are also "hoisted," they aren't initialized. They stay in a "dead zone" from the start of the block until the moment the code actually hits the declaration. The Lesson: JavaScript isn't just reading your code; it's preparing for it. Understanding the Execution Context makes debugging feel like having X-ray vision. 🦸♂️ Have you ever been bitten by the Temporal Dead Zone, or do you still find yourself reaching for var out of habit? Let’s discuss! 👇 #JavaScript #WebDevelopment #CodingTips #Frontend #Programming101
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