A common misconception in JavaScript: “Objects are copied.” Let’s test that: let obj1 = { name: "John" }; let obj2 = obj1; obj2.name = "Doe"; console.log(obj1.name); // "Doe" At first, this feels unexpected. But here’s what’s really happening: JavaScript doesn’t copy the object. It copies the reference to that object. So both obj1 and obj2 point to the same memory location. That’s why changing one reflects in the other. This is one of the most common concepts tested in interviews — often with tricky variations. Understanding this clearly means you won’t rely on guesses anymore. 👉 I’ve broken this and similar concepts step-by-step in the full video (link in comments)
More Relevant Posts
-
𝗝𝗦 𝗦𝘁𝗿𝗶𝗻𝗴 𝗣𝗼𝗹𝘆𝗳𝗶𝗹𝗹𝘀 𝗔𝗻𝗱 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗧𝗶𝗽𝘀 Do you know how JavaScript methods work? A polyfill adds modern features to old browsers. You write the code when a browser lacks a feature. Built-in methods live in prototypes. Example: toUpperCase lives in String.prototype. Every string uses the same function. This saves memory. Strings are primitive types. Primitives do not have methods. JavaScript uses auto-boxing to solve this. It creates a temporary object. The method runs. The object disappears. Prepare these string problems for your next interview: - Reverse a string without built-in methods. - Check for palindromes. - Find the longest substring without repeating characters. Source: https://lnkd.in/dU88fG4S
To view or add a comment, sign in
-
𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗶𝗻 𝟮𝟬𝟮𝟲 JavaScript changed. Tools are better now. You still fall into old traps. Avoid these 5 mistakes. - Top-level await It is risky in libraries. It creates race conditions. Use a singleton pattern. This protects your bundle size. - The == operator Stop using ==. It uses confusing logic. It ruins type safety. Use === for all checks. - let in async loops let is block-scoped. Async loops capture live values. Your logs will show the same number. Use a const inside the loop. - The this keyword Arrow functions do not bind this. They use the outer scope. This leads to undefined. Use regular functions for methods. - JSON and Dates JSON.parse returns strings. It does not create Date objects. Your code will crash. Use a reviver function. Source: https://lnkd.in/g4yDDjNe
To view or add a comment, sign in
-
𝗦𝗽𝗿𝗲𝗮𝗱 𝗩𝗦 𝗥𝗲𝘀𝘁 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿𝘀 𝗜𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 You've seen ... in JavaScript and wondered what it does. The same syntax is used for two purposes: - Spread operator expands values - Rest operator collects values Understanding this difference is crucial for writing clean JavaScript. The spread operator is used to expand elements from arrays or objects. It spreads elements individually and creates a shallow copy, avoiding mutation. The rest operator is used to collect multiple values into one. It gathers everything into an array. Here are key differences: - Purpose: Spread expands, Rest collects - Usage: Spread on the right, Rest on the left - Output: Spread gives individual elements, Rest gives an array Mastering spread vs rest helps you write cleaner code, handle data efficiently, and solve interview problems confidently. Source: https://lnkd.in/gmFfBhXX
To view or add a comment, sign in
-
𝗦𝗽𝗿𝗲𝗮𝗱 𝗩𝗦 𝗥𝗲𝘀𝘁 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿𝘀 𝗜𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 You've seen ... in JavaScript and wondered what it does. The same syntax is used for two purposes: - Spread operator expands values - Rest operator collects values Understanding this difference is crucial for writing clean JavaScript. The spread operator expands elements from arrays or objects. It's like opening a box and taking everything out. It spreads elements individually and creates a shallow copy, avoiding mutation. The rest operator collects multiple values into one. It's like gathering everything into a box. All arguments are collected into an array. Here are the key differences: - Spread operator expands values - Rest operator collects values - Spread operator is used on the right side, rest operator on the left side - Spread operator outputs individual elements, rest operator outputs an array You can use spread and rest operators to merge arrays, remove properties from objects, and more. Mastering spread and rest operators helps you write cleaner code, handle data efficiently, and solve interview problems confidently. Source: https://lnkd.in/gmFfBhXX
To view or add a comment, sign in
-
Day 22/100 of JavaScript Today’s topic: Prototypes in JavaScript JavaScript uses a prototype-based inheritance model. Every object in JavaScript has an internal link to another object called its prototype. Example function Person(name) { this.name = name; } Person.prototype.greet = function () { console.log("Hello " + this.name); }; const user = new Person("Apsar"); user.greet(); Here, "greet" is not inside the object itself, but shared through the prototype. 🔹Prototype chain If a property or method is not found on the object, JavaScript looks up the prototype chain. console.log(user.toString()); 👉 "toString()" comes from the base object prototype. 🔑 Key points - Functions have a "prototype" property. - Objects inherit from their prototype. - Helps in memory efficiency by sharing methods. #Day22 #JavaScript #100DaysOfCode
To view or add a comment, sign in
-
🚀 𝐃𝐚𝐲 𝟒 𝐨𝐟 𝐌𝐲 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠 𝐒𝐞𝐫𝐢𝐞𝐬 Today I learned about if-else (Conditions) in JavaScript 💡 👉 Conditions are used to make decisions in code. 📌 Syntax: if (condition) { // code runs if condition is true } else { // code runs if condition is false } 📌 Example: let age = 18; if (age >= 18) { console.log("You can vote"); } else { console.log("You cannot vote"); } 👉 Also learned about: else if → check multiple conditions 📌 Example: let marks = 75; if (marks > 90) { console.log("Grade A"); } else if (marks > 60) { console.log("Grade B"); } else { console.log("Grade C"); } 👉 Conditions help in building real-world logic 💻✨ 💬 Question: Have you used if-else in any project yet? Let’s learn together 🚀 #JavaScript #WebDevelopment #LearningInPublic #Day4 #FrontendDevelopment
To view or add a comment, sign in
-
-
🚀 Day 4/30 – JavaScript Challenge Solved: Counter II (LeetCode 2665) Today’s problem was all about understanding closures and how functions can maintain their own state in JavaScript. What I learned: 1.How closures help preserve variable values across function calls 2.Creating multiple operations (increment, decrement, reset) using a single function 3.Clean use of arrow functions for concise code Approach: I created a function that stores the initial value and returns an object with three methods: 1.increment() -> increases value 2.decrement() -> decreases value 3.reset() -> resets to initial value All of this works because of closure, where the inner functions still remember the variable n. Key Insight: Closures are powerful when you need to encapsulate data and control how it’s modified — a very common pattern in real-world JavaScript applications. Consistency is the real game here 🔥 Let’s keep building, one day at a time. #Day4 #30DaysOfCode #JavaScript #WebDevelopment #CodingChallenge #LeetCode #Closures #LearningJourney
To view or add a comment, sign in
-
-
Day 23/100 of JavaScript Today’s topic : "this" keyword "this" refers to the object that is currently executing the function But its value depends on how the function is called, not where it is written 🔹In object method const user = { name: "Apsar", greet() { console.log(this.name); } }; user.greet(); // Apsar 🔹In regular function function show() { console.log(this); } show(); // global object (or undefined in strict mode) 🔹Arrow function const obj = { name: "Apsar", greet: () => { console.log(this.name); } }; obj.greet(); // undefined 👉 Arrow functions don’t have their own "this", they inherit it 🔹call, apply, bind function greet() { console.log(this.name); } const user = { name: "Apsar" }; greet.call(user); #Day23 #JavaScript #100DaysOfCode
To view or add a comment, sign in
-
Day 7/100 of JavaScript 🚀 Today’s Topic: Taking input in JavaScript. In browser-based JavaScript, input can be taken using: - "prompt()" - Input fields (DOM) On platforms like LeetCode, input is already provided as function parameters Example: var twoSum = function(nums, target) { // input is already given }; So the focus is on writing logic, not handling input However, some coding platforms (or local environments) do not provide inbuilt input handling. In such cases, we use Node.js Example: process.stdin.on("data", (data) => { const input = data.toString().trim(); console.log(input); }); This allows us to read input from the console Key understanding: - Platforms like LeetCode handle input internally - Other platforms and local setups require manual input handling using Node.js Understanding both approaches is important for coding practice #Day7 #JavaScript #100DaysOfCode
To view or add a comment, sign in
-
𝗧𝗵𝗲 𝗝𝗮𝗩𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗥𝘂𝗻𝘁𝗶𝗺𝗲: 𝗙𝗶𝘅𝗶𝗻𝗴 𝗬𝗼𝘂𝗿 𝗠𝗲𝗻𝘁𝗮𝗹 𝗠𝗼𝗱𝗲𝗹 You may have noticed some behaviors in JavaScript that seem strange. For example: - setTimeout doesn't interrupt loops - setTimeout doesn't block - A resolved Promise still runs after synchronous code - await pauses a function but doesn't freeze the page - Rendering sometimes waits Let's explore why this happens. JavaScript executes synchronously inside a task. Nothing can interrupt that execution. Here's what we mean by 'synchronous' and 'asynchronous': - Synchronous execution: code that runs immediately, from top to bottom via the call stack - Asynchronous code: code whose result is not available immediately, it schedules something to run later Asynchronous mechanisms do not block nor interrupt the call stack. They arrange for something to run later via scheduling. Let's test this claim with a simple for loop and setTimeout: ``` is not allowed, so we use plain text instead console.log("sync start") for (let i = 0; i < 1e9; i++) {} console.log("sync end") console.log("sync start") setTimeout(() => { console.log("timeout fired") }, 0) for (let i = 0; i <
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
Full Video: https://youtu.be/GUQIfc3DiBA?si=CotojEbIK-5_cpnW