Small JavaScript language features that can save several lines of code. new Set() Set is a collection of unique values (it does not allow duplicates). const numbers = [1, 2, 2, 3, 4, 4] const unique = new Set(numbers) // [1, 2, 3, 4] Very useful for removing duplicate values from arrays in a quick and readable way. It also allows some operations such as: // Adds the value set.add(value) // Checks if the value exists set.has(value) // Deletes the value set.delete(value) --- Math.max() Used to find the largest number among the given values. Math.max(10, 5, 8) // 10 With an array and the spread operator: const numbers = [10, 5, 8] Math.max(...numbers) // 10 --- Putting both together. const numbers = [1, 5, 5, 3, 9, 1] // Removes duplicates and gets the largest value in a single line. const maior = Math.max(...new Set(numbers)) // 9 #JavaScript #TypeScript #WebDevelopment #FrontendDevelopment #ProgrammingTips #CleanCode #CodeQuality #SoftwareDevelopment #DevTips #LearnToCode #100DaysOfCode
JavaScript Set and Math.max for Code Efficiency
More Relevant Posts
-
💡 The JavaScript .sort() Surprise: Why [10, 2, 5] isn't [2, 5, 10] Ever had code that worked perfectly until the numbers got bigger? Check out this classic JavaScript quirk. 😅 In the screenshot, you'll notice: ✅ [3, 1, 4, 2].sort() results in [1, 2, 3, 4] (Perfect!) ❌ [10, 2, 5].sort() results in [10, 2, 5] (Wait... what?) The "Why" behind the weirdness: By default, JavaScript’s .sort() method converts elements into strings and compares their UTF-16 code unit values. It’s not looking at the numeric value; it’s looking at the characters. Just like "Apple" comes before "Banana," the string "10" comes before "2" because "1" comes before "2" in the dictionary. The Fix: To sort numbers correctly, you need to provide a compare function. This tells JavaScript exactly how to handle the math: let arr2 = [10, 2, 5]; // The correct way to sort numerically: arr2.sort((a, b) => a - b); console.log(arr2); // [2, 5, 10] tandard behavior is great for strings, but for data structures and algorithms (DSA), the compare function is your best friend! Have you ever been bitten by a default behavior in a programming language? Let’s hear your "favorite" bug in the comments! 👇 #JavaScript #WebDevelopment #CodingTips #Programming #SoftwareEngineering #DSA #LearningToCode
To view or add a comment, sign in
-
-
𝗧𝗵𝗲 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝘁𝗵𝗮𝘁 𝗡𝗲𝘃𝗲𝗿 𝗙𝗼𝗿𝗴𝗲𝘁𝘀: 𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗖𝗹𝗼𝘀𝘂𝗿𝗲𝘀 🧠 Hi everyone! I’ve just released Part 5 of my JavaScript deep-dive series: 𝗧𝗵𝗲 𝗠𝗲𝗰𝗵𝗮𝗻𝗶𝗰𝘀 𝗼𝗳 𝗖𝗹𝗼𝘀𝘂𝗿𝗲𝘀. Closures are often treated like a "magic trick" in JavaScript interviews, but they are actually a logical result of how the engine links scopes together. If you’ve ever struggled with why a setTimeout in a loop logs the "wrong" number, or how "private" variables actually work in a language without a private keyword, this article is for you. 𝗜𝗻 𝘁𝗵𝗶𝘀 𝗽𝗮𝗿𝘁, 𝗜 𝗱𝗶𝘃𝗲 𝗶𝗻𝘁𝗼: • 𝗧𝗵𝗲 𝗜𝗻𝘁𝗲𝗿𝗻𝗮𝗹 𝗦𝗹𝗼𝘁𝘀: Meet [[Environment]] and [[OuterEnv]], the two hidden connectors that make closures possible. • 𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 𝘃𝘀. 𝗩𝗮𝗹𝘂𝗲𝘀: Why closures link to "live" variables (and why that leads to the famous loop trap). • 𝗧𝗵𝗲 𝗜𝗜𝗙𝗘 𝘃𝘀. 𝗟𝗲𝘁: How we used to fix closures in the old days vs. the modern ES6 solution. • 𝗘𝗻𝗰𝗮𝗽𝘀𝘂𝗹𝗮𝘁𝗶𝗼𝗻: How to use closures to create "vaults" for your data. Stop guessing how your functions remember data and start understanding the architecture behind it! Read the full article here: https://lnkd.in/dR_TngGA 🔗 𝗡𝗲𝘅𝘁 𝘂𝗽: We tackle the "Grand Design" of JavaScript objects, 𝗣𝗿𝗼𝘁𝗼𝘁𝘆𝗽𝗲𝘀. #JavaScript #SoftwareEngineering #WebDevelopment #Coding #Closures #ProgrammingTips #TechCommunity #ScopeChain
To view or add a comment, sign in
-
JavaScript Destructuring: The Art of Unpacking Smarter! "Destructuring is a powerful JavaScript expression that lets you extract data from arrays or objects and assign them to distinct variables in a single, clean line". 1. Array Destructuring:Instead of accessing indices one by one, we "unpack" the entire array at once. const programmingLangs = ["C programming", "C++", "Java", "Python", "Js", "R"]; This is old method const a = programmingLangs[0]; const b = programmingLangs[1]; This is modern method const [first, second, third, ...others] = programmingLangs; console.log(first); console.log(second); console.log(others); 2. Object destructuring: const student={name: "vaseem", marks: 33, division:"3rd"}; const {name, marks}=student; console.log(name, marks); #JavaScript #WebDevelopment #CodingTips #ES6 #CleanCode #Programming #ReactJS
To view or add a comment, sign in
-
-
🤔 Quick JavaScript Challenge! What does this return? 0.1 + 0.2 === 0.3 If your answer is true, you might want to sit down for this one 😄 👉 It actually returns false. 0.1 + 0.2 // 0.30000000000000004 💡 Why does this happen? JavaScript uses floating-point arithmetic (IEEE-754) to store numbers. Some decimals like 0.1 and 0.2 can’t be represented exactly in binary, so they’re stored as very close approximations. When added together, the tiny errors show up. It’s not a bug — it’s how computers handle floating numbers! 🚀 What this teaches us as developers ✅ Never blindly trust floating-point math ✅ Use rounding when needed (toFixed, Math.round) ✅ Compare with a tolerance (Number.EPSILON) ✅ For finance apps, store values as integers (like cents) Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON 😄 Fun thought: If 0.1 + 0.2 isn’t 0.3, imagine what other “simple truths” in coding are secretly complicated. That’s what makes programming fun — always something new to learn! #ReactJS #JavaScript #WebDevelopment #Frontend #MERN #ReactHooks #CleanCode #JavaScript #WebDevelopment #FrontendMagic #CodeWithFun #TechExplainedSimply #mernstack #mern #react #js #JavaScript #WebDevelopment #CodingHumor #FrontendDevelopment #TechEducation #ProgrammingFun #LearnToCode #CodeNewbie #DeveloperCommunity
To view or add a comment, sign in
-
https://lnkd.in/gAPFk2j4 By reading this blogs : you got to now about - how to create array and ways of creating array in JavaScript - What are the data types and what are difference between them ? - How to do looping and searching in arrays - Explain about the most used array methods and which methods are mutable and immutable ? #cohort2026 #chaicode #JavaScript #Array
To view or add a comment, sign in
-
📘 Day 65: JavaScript Arrays & Powerful Array Methods 🔹 Array Basics: • Similar to lists — store multiple values in one variable • Can hold different datatypes • Mutable, ordered, and allow duplicates • Written inside square brackets [ ] 🔹 Concat: • Combines multiple arrays • Keeps duplicate values 🔹 Spread Operator (...): • Modern way to merge arrays • Cleaner and more flexible than concat • Very important in real projects 🔹 Push & Pop: • push() → adds items to the end • pop() → removes last item 🔹 Shift & Unshift: • shift() → removes first item • unshift() → adds item at the start 🔹 Join: • Converts array into string • Separator can be customized 🔹 Sort: • Sorts alphabetically or numerically • Numbers are sorted before strings 🔹 Slice: • Extracts part of an array • Doesn’t change original array • Supports negative indexing 🔹 Splice (Important): • Insert, replace, or delete elements • Changes original array • Uses position, delete count, and new values 💡 Practiced how arrays make data handling easier and how these methods help in real-world JavaScript development. #Day65 #JavaScript #WebDevelopment #CodingJourney #FrontendDevelopment #LearningJavaScript #Arrays #JSBasics
To view or add a comment, sign in
-
Working efficiently with arrays is essential in modern JavaScript development. These methods are fundamental tools for iterating and manipulating data. 1️⃣ map() – Transforming Arrays map() is used when you want to transform each element of an array and create a new array with the transformed values. Use Case: Modify values, extract object properties, apply calculations. 2️⃣ filter() – Selecting Arrays filter() is used when you want to select certain elements of an array that satisfy a specific condition. It returns a new array with only the elements that pass the test. Use Case: Filter users by age, tasks by completion status, or items by criteria. Both methods are immutable, meaning the original array remains unchanged. Mastering map() and filter() empowers developers to write more readable, professional, efficient, and maintainable JavaScript code. #JavaScript #WebDevelopment #Frontend #Programming #Coding #SoftwareDevelopment #LearningToCode
To view or add a comment, sign in
-
-
𝗛𝗮𝘃𝗲 𝘆𝗼𝘂 𝗲𝘃𝗲𝗿 𝘀𝘁𝗼𝗽𝗽𝗲𝗱 𝗮𝗻𝗱 𝗮𝘀𝗸 𝘆𝗼𝘂𝗿𝘀𝗲𝗹𝗳 𝘄𝗵𝘆 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗶𝘀 𝗽𝗮𝘀𝘀 𝗯𝘆 𝘃𝗮𝗹𝘂𝗲, 𝗮𝗻𝗱 𝗻𝗼𝘁 𝗽𝗮𝘀𝘀 𝗯𝘆 𝗥𝗲𝗳𝗲𝗿𝗲𝗻𝗰𝗲?🤔 You see... when a value is passed into a function in JS, the function receives a copy of that value, not ❌direct access to the original variable. This means a function cannot reassign the caller’s variable or take ownership of it. For primitive values like (numbers, strings, booleans...), the copied value is independent, so changes inside the function do not affect the original value. (e.g reassigning) For objects and arrays(non-primitives), the copied value happens to be a reference (memory address) to the same object. So, mutating the object inside the function affects the original object but reassigning the reference does not! 🚫 so... JavaScript always passes values, but for objects, the value being passed is a reference ✅✅. If you understand this, it will surely save you from debugging headaches 🧑💻🧑💻 . . . . . #JavaScript #CodingTips #WebDevelopment #Programming #LearnToCode #CodeNewbie #SoftwareEngineering #DevTips #TechCommunity
To view or add a comment, sign in
-
-
🚀 𝐓𝐡𝐞 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐋𝐨𝐨𝐩𝐢𝐧𝐠 𝐆𝐮𝐢𝐝𝐞 𝐟𝐨𝐫: The classic. Best when you know exactly how many times you need to run. 𝐟𝐨𝐫...𝐨𝐟: Modern and clean. Perfect for iterating over values in an array or string. 𝐟𝐨𝐫...𝐢𝐧: Use this for objects. It iterates over the keys (properties). 𝐰𝐡𝐢𝐥𝐞: Best when the number of iterations is unknown and depends on a condition. 𝐟𝐨𝐫𝐄𝐚𝐜𝐡(): The functional approach for arrays. Great for readability, though you can't "break" out of it early. // 1. The Standard For Loop for (let i = 0; i < 5; i++) { console.log(`Index: ${i}`); } // 2. For...Of (Best for Arrays) const fruits = ['🍎', '🍌', '🍇']; for (const fruit of fruits) { console.log(fruit); } // 3. For...In (Best for Objects) const user = { name: 'Alex', role: 'Dev' }; for (const key in user) { console.log(`${key}: ${user[key]}`); } // 4. While Loop (Condition-based) let energy = 3; while (energy > 0) { console.log("Coding..."); energy--; } 💡 𝐏𝐫𝐨 𝐓𝐢𝐩: If you are dealing with large datasets and need to transform them, consider array methods like .map() or .filter()—they are often more expressive than a standard loop! Feel free to reach me out for any career mentoring Naveen .G.R|CareerByteCode #javascript #webdevelopment #codingtips #programming #frontend
To view or add a comment, sign in
-
-
Most developers see this JavaScript pattern and ignore it: (function () { ... } )(); It’s called an IIFE (Immediately Invoked Function Expression) — a function that runs instantly. But here’s a real use case 👇 Imagine a webpage that needs to initialize analytics when it loads. You need some config values, helper functions, and setup logic — but you don’t want to pollute the global scope. (function () { const config = { trackingId: "UA-123456", env: "production" }; function initAnalytics() { console.log("Analytics started with:", config.trackingId); // load scripts, send page view, etc. } initAnalytics(); })(); Now: • Setup runs immediately on load • Config stays private • No global variables leaked • Other scripts stay safe Today, ES modules handle this better. But understanding IIFE helps you grasp scope, closures, and how JavaScript isolates logic. Old pattern. Still teaches powerful fundamentals. #JavaScript #WebDevelopment #Frontend #Programming #SoftwareEngineering
To view or add a comment, sign in
Explore related topics
- Code Planning Tips for Entry-Level Developers
- Coding Best Practices to Reduce Developer Mistakes
- Best Practices for Code Reviews in Software Teams
- How to Add Code Cleanup to Development Workflow
- Codebase Cleanup Strategies for Software Developers
- How to Refactor Code Thoroughly
- Traits of Quality Code Writing
- Principles of Elegant Code for Developers
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
Please update the image. You are spreading the Set into an array so “unique” becomes an array and you can’t use Set methods.