Swapping Two Numbers in JavaScript (3 Ways) Swapping values is a basic but important concept — and in JavaScript we have multiple ways to do it. ✅ In general, we can follow the first two ways (they’re common and good for understanding the logic). 🚀 But for clean, modern, and efficient code, the 3rd way is the best to proceed in JavaScript. 1) With a Third Variable (Most beginner friendly) let temp = a; a = b; b = temp; 2) Without Third Variable (Math trick) a = a + b; b = a - b; a = a - b; ⚠️ Note: Can be risky with very large numbers (overflow) and less readable. 3) Best in JavaScript: Destructuring Assignment ✅ (Efficient & Clean) [a, b] = [b, a]; This is the most readable, modern, and preferred way in JavaScript. #JavaScript #DSA #Programming #Coding #WebDevelopment #100DaysOfCode
KRISHNA TEJA MEDAM’s Post
More Relevant Posts
-
JavaScript Scope — The Foundation 🔹 JavaScript Scope Explained (Beginner → Pro) Scope defines where a variable is accessible in your code. JavaScript has three main scopes: Global Scope → Accessible everywhere Function Scope → Accessible only inside the function Block Scope (ES6) → Accessible only inside {} using let & const if (true) { let x = 10; }console.log(x); // ReferenceError 💡 Understanding scope helps you: ✔ Write cleaner code ✔ Avoid variable conflicts ✔ Debug faster Scope isn’t theory — it’s the backbone of JavaScript. #JavaScript #WebDevelopment #LearningJS #Programming
To view or add a comment, sign in
-
-
Most developers think they understand Hoisting.🙄 But ask them this: If let and const are hoisted then why does JavaScript throw a ReferenceError? 🤔 And here’s the bigger question: Why does var attach to window but let doesn’t? This is not beginner syntax anymore. This is about how the JavaScript engine actually prepares memory before execution. If you truly understand: • Memory Creation Phase • Global Execution Context • Temporary Dead Zone I’ve broken this concept down visually in a simple way. 🎯 Full breakdown is on my Instagram → @JswithDhruv Let’s build JavaScript fundamentals the right way. 📌 Save this for revision. Post Link: https://lnkd.in/dzUhxnNN #JavaScript #Programming #LearnToCode #ReactJs #connections #FrontendDevelopment
To view or add a comment, sign in
-
-
Strengthening my JavaScript fundamentals one concept at a time! Today I revisited essential string functionalities in JavaScript — simple methods, but extremely powerful in real-world development. From transforming text to searching, slicing, and splitting strings, these functions are used almost everywhere in frontend applications. ✨ Quick reminder: Clean code starts with strong basics. Consistent practice with fundamentals like string manipulation helps write more efficient logic, optimize performance, and handle data better in real projects. What’s one JavaScript method you use almost daily? 👇 #JavaScript #WebDevelopment #FrontendDeveloper #CodingJourney #LearnToCode #Programming #ReactDeveloper
To view or add a comment, sign in
-
-
🔒 Closures in JavaScript — Ever wondered how a function in JavaScript can remember variables even after execution is complete? That’s the power of Closures 🚀 👉 A closure is formed when a function bundles itself with its lexical scope. This allows inner functions to access outer variables, even when the outer function is no longer on the call stack. 💡 Why closures matter: ✔ Data encapsulation (private variables) ✔ setTimeout & callbacks ✔ Function factories ✔ Cleaner and more powerful code Closures are one of the most important concepts for interviews and real-world JavaScript development. If you truly understand closures, you understand how JavaScript works under the hood. Save this post 📌 and share it with someone learning JS! Nishant Pal #JavaScript #WebDevelopment #Frontend #Coding #LearnJavaScript #Programming #100DaysOfCode
To view or add a comment, sign in
-
-
Good JavaScript code isn’t about fancy syntax. It’s about clarity, maintainability, and smart decisions. A few habits that can instantly improve your JS code 👇 ✔ Use meaningful variable names ✔ Keep functions small and focused ✔ Comment why, not what ✔ Avoid callback hell — use async/await ✔ Write code for humans, not just machines Small improvements in JavaScript lead to cleaner code and fewer bugs over time. #JavaScript #WebDevelopment #FrontendDevelopment #CodingTips #CleanCode #Programming #SoftwareEngineering #DeveloperLife #CodeQuality
To view or add a comment, sign in
-
-
One JavaScript habit that improved my code instantly Earlier, my JavaScript files were long and confusing. Everything was written in one place… and debugging was painful. Then I started using small reusable functions instead of writing everything in one block. Now my code is: 1- easier to read 2-ceasier to understand 3- easier to test 4- easier to reuse Instead of repeating logic, I write it once and call it when needed. This one habit saved me time and reduced bugs in my projects. It reminded me of one thing: Clean code is not about writing more code. It’s about writing better code. Still learning and improving every day 🚀 What coding habit helped you the most? #JavaScript #CleanCode #WebDevelopment #FrontendDeveloper #LearningJourney #CodeTips
To view or add a comment, sign in
-
-
Stop Writing Ugly Conditions in JavaScript Messy conditional logic is one of the fastest ways to make code unreadable and error-prone. Deeply nested if-else statements, chained ternaries, repeated null checks — we’ve all written them. But clean, maintainable JavaScript requires better patterns. In this post, you’ll learn how to: Use guard clauses to reduce nesting Replace complex condition trees with cleaner logic Leverage optional chaining (?.) and nullish coalescing (??) Improve readability without sacrificing performance Writing better conditions isn’t about shorter code — it’s about clearer intent. Clean code scales. Ugly conditions don’t. #JavaScript #SoftwareEngineering #CleanCode #FrontendDevelopment #Programming
To view or add a comment, sign in
-
💡 JavaScript Basics: var, let, and const When learning JavaScript, one of the most common confusions is the difference between var, let, and const. The key differences come down to scope and reassignment. 🔹 var var is function-scoped and does not follow block {} scope. It can be redeclared, which often leads to unexpected bugs. 👉 In modern JavaScript, it’s generally best to avoid using var. 🔹 let let is block-scoped and allows reassignment. It’s ideal for loops and conditional logic where values may change. 🔹 const const is also block-scoped, but its value cannot be reassigned once declared. 👉 Best used for fixed values and safer, more predictable code. ✅ Best practice I follow: • Use const by default • Use let when reassignment is needed • Avoid var whenever possible Small fundamentals like these make a big difference in writing clean, reliable JavaScript. Still learning, still improving 🚀 #JavaScript #FrontendDevelopment #Programming #LearningJourney #WebDevelopment
To view or add a comment, sign in
-
-
🚨 JavaScript Logic Challenge What’s the Output? Think you’ve mastered core JavaScript? Let’s test your fundamentals 👇 Code: let x = "20"; let y = 5; console.log(x + y); 💬 What will be the output? (a) 25 (b) 205 (c) "100" (d) Error This looks simple… but it tests your understanding of: ✅ Type Coercion ✅ Data Types ✅ String vs Number operations ✅ The + operator behavior in JavaScript Many developers get this wrong because they forget how JavaScript handles implicit conversion. 👇 Drop your answer in the comments (a, b, c, or d) 🔥 Tag your coding buddy 📌 Save this post if you love JS challenges Let’s see who really understands JavaScript fundamentals. #JavaScript #WebDevelopment #FrontendDeveloper #CodingChallenge #LearnToCode #100DaysOfCode #JS #Programming #DeveloperCommunity #TechCareers #reels #explore #viral #coding #fyp
To view or add a comment, sign in
-
Day 10/30 – JavaScript Once Function 🧠 | Ensure a Function Runs Only Once💻🚀 🧠 Problem: Given a function fn, return a new function that: Executes fn only once Returns the result on the first call Returns undefined on all subsequent calls ✨ This challenge helped me understand: Closures & state preservation Function wrappers How real-world features like one-time events, initialization logic, and API guards work This pattern is commonly used in: ⚡ Event listeners ⚡ Authentication flows ⚡ Performance optimization 💬 Where would you use a “run once” function? Comment below 👇 #JavaScript #30DaysOfJavaScript #CodingChallenge #Closures #JSLogic #LeetCode #WebDevelopment #LearnToCode #CodeEveryday #DeveloperJourney #Programming #TechCommunity JavaScript once function Call function only once JS JavaScript closures example Higher order functions JavaScript LeetCode JavaScript solution JS interview questions Beginner JavaScript practice Daily coding challenge
To view or add a comment, sign in
-
Explore related topics
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