If strings in JavaScript are primitives, how can we call methods like .split()? "hello".split("") At first this looks strange, because primitives are not objects. The reason it works is because JavaScript does something behind the scenes called auto-boxing. When you call a method on a string, JavaScript temporarily wraps the primitive into a String object, runs the method, and then removes the object. That’s why primitives like strings can still use methods. And this also explains this behavior "hello" === new String("hello") // false Because one is a primitive and the other is an object. #javascript
JavaScript Auto-Boxing Explained: Calling Methods on Primitives
More Relevant Posts
-
Most JS bugs come from one thing — not knowing when JavaScript secretly converts your types for you. New blog post → Type Casting vs Type Coercion in JavaScript Covers: • What coercion actually is • How to write safer code https://lnkd.in/dQMaKZbH #javascript #WebDev #100DaysOfCode
To view or add a comment, sign in
-
🧠 JavaScript Concept: Hoisting Explained Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their scope before execution. This is why we can sometimes use variables or functions before they are declared. Example: console.log(name); // undefined var name = "Arun"; Why undefined? Because JavaScript internally treats it like this: var name; console.log(name); name = "Arun"; 🔹 Important Points: • "var" is hoisted and initialized with "undefined" • Function declarations are fully hoisted • "let" and "const" are hoisted but stay in the Temporal Dead Zone (TDZ) ⚠️ Accessing "let" or "const" before declaration will throw an error. 📌 Best Practice: Avoid relying on hoisting — always declare variables at the top for better readability. #javascript #frontenddevelopment #reactjs #webdevelopment #coding
To view or add a comment, sign in
-
-
Understanding Currying in JavaScript Ever heard of currying? It's a cool technique that makes your code cleaner and more reusable! What is Currying? Currying is when you break down a function that takes multiple arguments into a series of functions that each take one argument. Simple Example: Instead of: javascript function add(a, b, c) { return a + b + c; } add(1, 2, 3); // Output: 6 You write: javascript function add(a) { return function(b) { return function(c) { return a + b + c; } } } add(1)(2)(3); // Output: 6 Why Use It? Reusability: Create specialized functions easily Cleaner code: Break complex logic into smaller pieces Function composition: Combine functions in powerful ways Real-world benefit: javascript const addFive = add(5); addFive(2)(3); // Output: 10 Now you have a function that always adds 5 first! #javascript #webDeveloper #uiDeveloper #frontendDeveloper #react #reactjs
To view or add a comment, sign in
-
💡 JavaScript Trick Question: 3 + 2 + "7" In JavaScript, the answer is: 👉 "57" 🔍 Why? 🔹 JavaScript follows left-to-right evaluation and uses type coercion. ⚡ Key Insight : 🔹Once a string enters the expression, everything after that becomes a string operation. "In JavaScript, the moment a string joins the party, numbers stop adding and start concatenating." #JavaScript #WebDevelopment #CodingInterview #Frontend #JSConcepts
To view or add a comment, sign in
-
-
🚀 New Blog: Async/Await in JavaScript Clean async code ≠ magic It’s just promises made readable 🔗 https://lnkd.in/d4yaQjBa Also check this JS revision repo 🔗 https://lnkd.in/dqR-5ytF I revised callbacks, promises & async/await here — cleared a lot of my confusion 🔥 #javascript #webdev #asyncawait Hitesh Choudhary Piyush Garg Akash Kadlag Suraj Kumar Jha
To view or add a comment, sign in
-
I’ve watched so many explanations about arrow functions in JavaScript… and almost all of them say the same thing: “Arrow functions are just shorter syntax.” That never felt right to me. Because if it was just syntax, JavaScript wouldn’t need a whole new function type. The real difference is not how they look. It’s how they behave. In JavaScript, normal functions don’t “remember” "this". They wait until they are called… and then decide what "this" should be. So the same function can behave differently depending on how you invoke it. That’s where most confusion actually comes from. Arrow functions changed this completely. They don’t create their own "this". Instead, they capture "this" from the surrounding scope at the time they are created. And once captured, it never changes. So the real difference is simple: Normal function → "this" is decided at call time Arrow function → "this" is fixed at creation time So no, arrow functions are not just about shorter syntax. They are about removing uncertainty. Once you understand this, a lot of JavaScript weirdness suddenly starts making sense. Full breakdown: https://lnkd.in/gVdMH_42
To view or add a comment, sign in
-
Comparing Sets in JavaScript With difference() and symmetricDifference() How the new Set methods difference() and symmetricDifference() can replace verbose Set comparisons with clear, expressive code. https://lnkd.in/dZT4jg-4 #javascript
To view or add a comment, sign in
-
Understanding async is a turning point in JavaScript. Just published a beginner-friendly blog on Synchronous vs Asynchronous JS ✨ 👉 https://lnkd.in/g-RB5ZU2 Chai Aur Code Hitesh Choudhary Piyush Garg Akash Kadlag Anirudh Jwala #chaicode #javascript
To view or add a comment, sign in
-
-
Garbage Collection in JavaScript — Simplified Garbage Collection (GC) is the process by which JavaScript automatically frees memory that is no longer in use. Anything reachable from the root (window in browsers, global in Node.js) is kept in memory. Anything not reachable becomes garbage. JavaScript uses the Mark and Sweep algorithm: 1. Mark all objects reachable from the root 2. Sweep (remove) all objects that are not marked Example: let user = { name: "Shreyas" }; // memory allocated user = null; // object becomes unreachable Now, since there is no reference to the object, it becomes eligible for garbage collection. Understanding this helps in writing memory-efficient JavaScript applications. #JavaScript #WebDevelopment #FrontendDevelopment #Performance
To view or add a comment, sign in
-
Got a minute for some JavaScript? 🍵 What does this code output? Answers 🔍 >>> - ReferenceError: message is not defined Why? Because let lives only inside the block where it’s created. In this code, message is created inside the *if {}* and *else {}* blocks. When JavaScript reaches *console.log(message)*, it is already outside those blocks, so the variable no longer exists. #javascript #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