✅ JavaScript Output — Why This String Didn’t Change This morning’s code was: let a = "Hello"; a[0] = "h"; console.log(a); console.log(a.length); 💡 Correct Output Hello 5 🧠 Simple Explanation : 🔹 Strings are immutable in JavaScript That means: Once a string is created, you cannot change its characters directly. So when you do: a[0] = "h"; JavaScript ignores this operation. No error. No warning. Just no change. That’s why: console.log(a); still prints: Hello 🔹 Why does a[0] look like it should work? Because strings allow read access: a[0] // "H" But write access is not allowed. Strings are not arrays. 🔹 What about a.length? Since the string never changed, its length remains: 5 ✔ Output: 5 🎯 Key Takeaways : Strings are immutable You can read characters but cannot modify them To change a string, you must create a new one ✔ Correct way: a = "h" + a.slice(1); 💬 Your Turn Did you expect the string to change? 😄 Comment “I thought it would 😮” or “Knew this 👍” #JavaScript #FrontendDevelopment #LearnJS #CodingInterview #Strings #TechWithVeera #WebDevelopment #100DaysOfCode
JavaScript Strings Are Immutable
More Relevant Posts
-
What is Scope Chain in JavaScript? Understanding how JavaScript looks for variables helps everything make more sense. 🔹 Knowing why inner functions can access outer variables 🔹 Debugging undefined issues with confidence 🔹 Writing clean, predictable and bug-free JS code ❌ The code below can be confusing without understanding the Scope chain let x = 50; function outerFunction() { function innerFunction() { console.log(x); // Where does x come from? } innerFunction(); } outerFunction(); // output 50 ✅ The code below works because of the Scope Chain let x = 10; function outerFunction() { let y = 20; function innerFunction() { console.log(x, y); } innerFunction(); } outerFunction(); // output 10 20 Scope chain follow some steps that are listed below. 1️⃣ First, it looks in the current scope 2️⃣ Then, it checks the outer (parent) scope 3️⃣ This continues up to the global scope 4️⃣ If the variable is not found, JavaScript throws a ReferenceError ✅ Key takeaway: Inner functions can access variables from their outer scopes because of the scope chain. #JavaScript #ScopeChain #JSConcepts #WebDevelopment #FrontendDevelopment #LearnJavaScript #SoftwareDevelopment #DeveloperTips
To view or add a comment, sign in
-
So, JavaScript is all about functions and objects. It's like the foundation of the whole language. You gotta understand how they work. A function is basically a block of code that does something - and you can use it over and over. Simple. But here's the thing: there are a few ways to create functions in JavaScript. You've got your function declaration, function expression, and arrow function - each with its own twist. For example, you can do something like console.log(greet("Ajmal")) - and that's a function in action. Or, you can create a function like const add = function (a, b) { return a + b; } - and that's another way to do it. And then there's the arrow function, like const multiply = (a, b) => a * b - which is pretty cool. Now, objects are a different story. They're like containers that store data in key-value pairs - which is really useful for managing state and behavior. You can think of an object like a person - it's got characteristics (like name, age, etc.) and actions (like walking, talking, etc.). So, objects are all about combining state and behavior in a neat little package. And that's why you can use them to store and manage data in a really efficient way. Check out this article for more info: https://lnkd.in/gTMkkSGP #JavaScript #Functions #Objects
To view or add a comment, sign in
-
✅ Why Changing arguments Changes the Parameter This morning’s code was: function demo(a, b) { arguments[0] = 100; console.log(a); } demo(10, 20); 💡 Correct Output 100 🧠 Simple Explanation : 🔹 Step 1: Function call demo(10, 20); a = 10 b = 20 arguments becomes: arguments = { 0: 10, 1: 20 } 🔹 Step 2: Modify arguments arguments[0] = 100; Here’s the key rule 👇 👉 In non-strict mode, parameters and arguments are linked So when you change: arguments[0] JavaScript also updates: a 🔹 Step 3: Console output console.log(a); Since a is now updated to 100, output is: 100 🎯 Key Takeaways : arguments is not an array In non-strict mode: arguments and parameters are linked Modifying arguments[index] can change parameters This does NOT happen in: strict mode arrow functions 📌 That’s why modern JavaScript prefers rest parameters: function demo(...args) {} 💬 Your Turn Did you expect 10 or 100? 😄 Comment “Tricky but clear 👍” or “Didn’t know this 🤯” #JavaScript #LearnJS #FrontendDevelopment #CodingInterview #Functions #TechWithVeera #WebDevelopment
To view or add a comment, sign in
-
-
Day 3: JavaScript Hoisting — Magic or Logic? Yesterday, we learned that JavaScript creates memory for variables before it executes the code. Today, let’s see the most famous (and sometimes confusing) result of that: Hoisting. What is Hoisting? Hoisting is a behavior where you can access variables and functions even before they are initialized in your code without getting an error. Wait... why didn't it crash? 🤔 If you try this in other languages, it might throw an error. But in JS: During Memory Phase: JS saw var x and gave it the value undefined. It saw the function getName and stored its entire code. During Execution Phase: When it hits console.log(x), it looks at memory and finds undefined. When it hits getName(), it finds the function and runs it! ⚠️ The "Let & Const" Trap Does hoisting work for let and const? Yes, but with a catch! They are hoisted, but they are kept in a "Temporal Dead Zone". If you try to access them before the line where they are defined, JS will throw a ReferenceError. Pro Tip: Always define your variables at the top of your scope to avoid "Hoisting bugs," even though JS "magically" handles them for you. Crucial Takeaway: Hoisting isn't code physically moving to the top; it’s just the JS Engine reading your "ingredients" before "cooking" (Phase 1 vs Phase 2). Did you know about the Temporal Dead Zone? Let me know in the comments! #JavaScript #WebDev #100DaysOfCode #Hoisting #CodingTips #FrontendDeveloper
To view or add a comment, sign in
-
-
✅ JavaScript Output — Why This Date Confuses Almost Everyone This morning’s code was: const date = new Date(2025, 1, 1); console.log(date.getMonth()); console.log(date.getDate()); console.log(date.getFullYear()); 💡 Correct Output 1 1 2025 Now let’s understand why this happens 👇 🧠 Simple Explanation : 🔹 How new Date(year, month, day) really works JavaScript’s Date constructor follows this rule: 👉 Month is ZERO-based That means: 0 → January 1 → February 2 → March … 11 → December So when you write: new Date(2025, 1, 1) JavaScript reads it as: 👉 1st February 2025, not January. 🔹 Line 1: date.getMonth() Since February is internally stored as 1: getMonth() → 1 ✔ Output: 1 🔹 Line 2: date.getDate() This returns the day of the month, and here it is clearly: 1 ✔ Output: 1 🔹 Line 3: date.getFullYear() Year is straightforward: 2025 ✔ Output: 2025 🎯 Key Takeaways : JavaScript months are 0-based Days and years are 1-based new Date(2025, 1, 1) = Feb 1, 2025 This causes many real-world bugs if not remembered 📌 That’s why many developers prefer libraries or careful handling when working with dates 💬 Your Turn Did you expect 1 to mean February? 😄 Comment “Tricky 😮” or “Knew this 👍” #JavaScript #FrontendDevelopment #LearnJS #CodingInterview #Dates #TechWithVeera #WebDevelopment #100DaysOfCode
To view or add a comment, sign in
-
-
The only JavaScript function that comes with a "Pause" button. ⏯️ In JavaScript, the golden rule of functions is usually "Run-to-Completion." Once a function starts, it doesn't stop until it returns or finishes. 𝐄𝐧𝐭𝐞𝐫 𝐭𝐡𝐞 𝐆𝐞𝐧𝐞𝐫𝐚𝐭𝐨𝐫 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧. 🤯 It completely breaks the rule. It is a special type of function that can be paused in the middle of execution and resumed later from exactly where it left off. 𝐓𝐡𝐞 𝐌𝐚𝐠𝐢𝐜 𝐒𝐲𝐧𝐭𝐚𝐱: 1️⃣ `function*`: The asterisk tells JS this isn't a normal function. 2️⃣ `yield`: This keyword acts like a pause button. It spits out a value and freezes the function's state. 3️⃣ `.next()`: This is the play button. Call it to resume the function until it hits the next `yield`. 𝐖𝐡𝐲 𝐢𝐬 𝐭𝐡𝐢𝐬 𝐮𝐬𝐞𝐟𝐮𝐥? (𝐋𝐚𝐳𝐲 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐢𝐨𝐧) Generators allow for 𝐋𝐚𝐳𝐲 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐢𝐨𝐧. You don't have to calculate a list of 1 million items at once (crashing your memory). You can generate them one by one, only when you actually need them. It’s perfect for infinite streams, ID generators, or defining complex state machines. Check out the syntax breakdown below! 👇 Have you used Generators in production (maybe with Redux Saga)? #JavaScript #WebDevelopment #CodingPatterns #AdvancedJS #SoftwareEngineering #Frontend
To view or add a comment, sign in
-
-
So, you wanna get a grip on JavaScript. It's all about functions and objects, really. They're the building blocks. A function is like a recipe - it's a set of instructions that does something, and you can use it over and over. You can make functions in a few different ways: - the classic function declaration, - function expressions, which are kinda like assigning a function to a variable, - and then there's the arrow function, which is like a shorthand way of doing things. For example, you can use functions like this: console.log(greet("Ajmal")); - it's like calling a friend to say hello. Or, you can create a function that adds two numbers together, like this: const add = function (a, b) { return a + b; }; - it's basic, but it works. And then there's the arrow function, which is super concise, like this: const multiply = (a, b) => a * b; - it's like a quick math trick. Objects, on the other hand, are like containers - they store data in key-value pairs, so you can keep things organized. It's like having a toolbox, where you can store all your functions and data in one place. You can use objects to make your code more manageable, and that's a beautiful thing. Check out this article for more info: https://lnkd.in/gTMkkSGP #JavaScript #Functions #Objects
To view or add a comment, sign in
-
JavaScript Scope Chain — Explained Simply (No Fluff) If you’ve ever wondered “Where the hell did this variable come from?”, this is for you. Understanding the scope chain explains: Why inner functions can access outer variables Why undefined or ReferenceError happens How JavaScript actually resolves variables ❌ Confusing without scope chain let x = 50; function outerFunction() { function innerFunction() { console.log(x); // Where does x come from? } innerFunction(); } outerFunction(); // 50 ✅ Clear when you know the rule let x = 10; function outerFunction() { let y = 20; function innerFunction() { console.log(x, y); } innerFunction(); } outerFunction(); // 10 20 🔍 How JavaScript finds variables 1️⃣ Look in the current scope 2️⃣ Move to the parent scope 3️⃣ Continue up to the global scope 4️⃣ Not found? → ReferenceError Key takeaway: Inner functions don’t magically get variables — JavaScript walks up the scope chain until it finds them. If you don’t understand scope, you’ll write unpredictable JS. If you do, debugging becomes boring — and that’s a good thing. #JavaScript #WebDevelopment #Frontend #ReactJS #Programming #ScopeChain #CleanCode
To view or add a comment, sign in
-
🧠 JavaScript Quiz — What will this log? const user = { name: "Ali", greet() { console.log(`Hi, ${this.name}`); } }; const greet = user.greet; greet(); ❓ What will be printed to the console? A) Hi, Ali B) Hi, undefined C) Throws an error D) Nothing ⏳ Think before scrolling… ✅ Correct answer: B) Hi, undefined 📌 Why? In JavaScript, this depends on how a function is called, not where it’s defined. When the method is assigned to a variable, it loses its object context, so this.name becomes undefined. 🛠 Fix: const greet = user.greet.bind(user); greet(); // Hi, Ali 💡 Takeaway: Method extraction breaks this unless you bind it. Have you faced this this behavior in real projects? 👀 #Javascript #JSQuiz #Quiz #SWE #CriticalThinking #Growth
To view or add a comment, sign in
-
-
🚀 5 Tricky JavaScript Output Questions (Closures & Scope) Let’s test your JS fundamentals 👇 Try to guess the output before checking answers. 1️⃣ for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); } 2️⃣ let a = 10; (function () { console.log(a); let a = 20; })(); 3️⃣ function foo() { console.log(x); var x = 10; } foo(); 4️⃣ const obj = { a: 10, getA() { return this.a; } }; const fn = obj.getA; console.log(fn()); 5️⃣ console.log(typeof typeof 1); Answers below 👇 1️⃣ 3 3 3 (var is function-scoped, same reference) 2️⃣ ReferenceError (Temporal Dead Zone for `let`) 3️⃣ undefined (var hoisting without initialization) 4️⃣ undefined (`this` is lost when function is detached) 5️⃣ "string" (typeof 1 → "number", typeof "number" → "string") If you can explain *why*, you’re interview-ready 💯 #javascript #frontend #interviewprep #webdevelopment
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