Why I reach for Map in JavaScript (and you should too) If you're still using plain objects for every key-value need, try Map next time — especially when your keys aren’t simple strings or when performance and predictable iteration order matter. // Quick JavaScript Map example const users = new Map(); // set users.set(1, { name: "Asha", role: "Designer" }); users.set(2, { name: "Ravi", role: "Developer" }); // get console.log(users.get(1)); // { name: "Asha", role: "Designer" } // size, has, delete console.log(users.size); // 2 console.log(users.has(2)); // true users.delete(2); // iterate for (const [id, user] of users) { console.log(id, user.name); } // convert to array const arr = Array.from(users.entries()); When to choose Map: 1. Keys can be anything (objects, functions, primitives). 2. You need guaranteed insertion order during iteration. 3. You want faster operations for frequent add/remove compared to large-object hacks. Pro tip: Use Map for caches, metadata stores, or when keys are non-string references. For simple JSON-like data or when you need JSON.stringify, stick with plain objects. Have you used Map in a real project? What problem did it solve for you? 👇 #javascript #webdev #frontend #programming #codingtips
Why use Map in JavaScript for key-value pairs
More Relevant Posts
-
Let’s understand Loops and Conditions in EJS Template Files (Backend Series) When rendering data dynamically in EJS (Embedded JavaScript Templates), you often need to loop through arrays or use conditional logic to display content smartly, just like you would in JavaScript. EJS allows you to write pure JavaScript syntax directly inside your HTML files using special tags: • <% %> for logic (like loops or conditions) • <%= %> for output (to display values) Here’s how it works step by step: 1️⃣ Looping through data: If your backend sends an array of users like res.render("users", { users: ["Moeez", "Ali", "Sara"] }); You can display them dynamically in your EJS file: <ul> <% users.forEach(user => { %> <li><%= user %></li> <% }) %> </ul> Each item from the array is rendered as a list element, no manual repetition needed. 2️⃣ Using conditions: You can easily handle scenarios where data might or might not exist. <% if (users.length > 0) { %> <p>Total Users: <%= users.length %></p> <% } else { %> <p>No users found</p> <% } %> This ensures your UI responds to backend logic dynamically and cleanly. 3️⃣ Why it matters: • Keeps templates clean and logic-driven. • Reduces repetitive HTML. • Lets you manage data directly in the view layer. • Perfect for rendering lists, tables, dashboards, or dynamic content. EJS basically turns your HTML into a smart, data-aware view, making your backend responses look like complete web pages instead of static output. #Nodejs #Expressjs #EJS #TemplateEngine #BackendDevelopment #WebDevelopment #ServerSideRendering #FullStackDeveloper #JavaScript #NodeDeveloper #WebApps #Coding #LearningNodejs #SoftwareEngineer
To view or add a comment, sign in
-
⚡ Write Cleaner Logic with JavaScript Conditionals ✨ Conditionals allow JavaScript to make decisions and execute different code based on different conditions. They are foundational in controlling application logic and user flows. 🔹 1️⃣ if / else — Basic Decision Making Used when you want to execute code based on a true/false condition. if (score >= 40) { console.log("Pass"); } else { console.log("Fail"); } 🔹 2️⃣ else if — Multiple Conditions Useful when you need to check more than one condition in sequence. if (temp > 30) { console.log("Hot"); } else if (temp > 20) { console.log("Warm"); } else { console.log("Cold"); } 🔹 3️⃣ Ternary Operator — Short Form If/Else Compact and commonly used for simpler conditions. let result = age >= 18 ? "Eligible" : "Not Eligible"; ✔ Best for simple expressions ✔ Avoid using ternary for long or nested logic 🔹 4️⃣ switch — Cleaner Multi-Condition Handling Ideal when comparing the same value against multiple possibilities. switch (role) { case "admin": console.log("Access Granted"); break; case "user": console.log("Limited Access"); break; default: console.log("No Access"); } 🔹 5️⃣ Truthy & Falsy Values JavaScript considers some values “false” automatically: Falsy: 0, "", null, undefined, NaN, false Everything else → truthy if (username) { console.log("Valid"); } 🔹 6️⃣ Short-Circuit Evaluation Efficient way to write compact conditions. isOnline && showStatus(); username || "Guest"; && runs the second expression only if the first is true || returns the first truthy value 📌 Professional Summary Use if/else for general decisions Use switch when one value is checked repeatedly Use ternary for simpler expressions Understand truthy/falsy to avoid unexpected behavior Short-circuiting helps create clean, concise logic #JavaScript #FrontendDevelopment #WebDevelopment #CleanCode #ES6
To view or add a comment, sign in
-
-
Reflection & Object Composition in Modern JavaScript In JavaScript, objects don’t just store values — they can look at themselves and manipulate their own properties. That’s the power of Reflection. Reflection enables patterns like Object Composition, a clean, modern alternative to long prototype chains. Reflection in Action const john = { firstname: "John", lastname: "Doe" }; for (let prop in john) { if (john.hasOwnProperty(prop)) { console.log(prop + ": " + john[prop]); } } Output: firstname: John lastname: Doe Object Composition — the ES6+ Way Instead of relying on libraries like Lodash or Underscore, modern JS gives us built-ins: 1️⃣ Object.assign() Object.assign(john, { address: "123 Main St" }, { getFirstName() { return this.firstname; } }); 2️⃣ Spread Operator (ES2018) const extendedJohn = { ...john, address: "123 Main St", getFirstName() { return this.firstname; } }; Object.assign() → Mutates the object Spread { ...obj } → Creates a new one Why It Matters No external libraries needed Clean, readable syntax Full control over mutation or immutability 100% native and widely supported Takeaway: With ES6+, Object.assign() and the spread operator { ...obj } give you all the power of extend, natively — Reflection + Composition, the modern way. #JavaScript #ES6 #FrontendDevelopment #WebDevelopment #CleanCode #ProgrammingTips #React #TypeScript
To view or add a comment, sign in
-
Why Map Lookups Are Slower Than Object Lookups in JavaScript Imagine this: you’re optimizing your JavaScript code and you notice something odd. You’re using a Map to store some configuration settings or feature flags, but when you benchmark it against a plain object, it feels slower. Both Map and Object provide O(1) lookups, so what’s happening under the hood? Let’s break it down. Many developers assume that Map is the modern, better alternative to Object for all key-value storage. And for certain cases, it absolutely is. But here’s the thing: if your keys are strings and the set of keys is relatively small and fixed, an Object is almost always faster. Why? Let’s dive into the mechanics. Consider this scenario: you have a set of API endpoints your application uses. // Using an Object const endpoints = { login: '/api/login', logout: '/api/logout', profile: '/api/profile' }; // Using a Map const endpointsMap = new Map([ ['login', '/api/login'], ['logout', '/api/logout'], ['profile', '/api/profile'] ]); Accessing a value: endpoints[' https://lnkd.in/g9SAXpuR
To view or add a comment, sign in
-
⚡ 𝗠𝗮𝘀𝘁𝗲𝗿 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗔𝗿𝗿𝗮𝘆𝘀 — 𝟴 𝗚𝗮𝗺𝗲-𝗖𝗵𝗮𝗻𝗴𝗶𝗻𝗴 𝗠𝗲𝘁𝗵𝗼𝗱𝘀 𝗘𝘃𝗲𝗿𝘆 𝗗𝗲𝘃 𝗦𝗵𝗼𝘂𝗹𝗱 𝗞𝗻𝗼𝘄 🚀 Arrays are the heartbeat of JavaScript. Whether you’re transforming data, filtering responses, or rendering UI — arrays are everywhere. But to write clean, optimized, and professional code, you need to go beyond basic loops. 𝗛𝗲𝗿𝗲 𝗮𝗿𝗲 𝟴 𝗮𝗿𝗿𝗮𝘆 𝗺𝗲𝘁𝗵𝗼𝗱𝘀 𝘁𝗵𝗮𝘁 𝘄𝗶𝗹𝗹 𝘁𝗮𝗸𝗲 𝘆𝗼𝘂𝗿 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝘀𝗸𝗶𝗹𝗹𝘀 𝘁𝗼 𝘁𝗵𝗲 𝗻𝗲𝘅𝘁 𝗹𝗲𝘃𝗲𝗹 👇 1️⃣ 𝗺𝗮𝗽() → Transforms each element and returns a new array. 🔹 Example: Rendering product cards or modifying fetched data. 2️⃣ 𝗳𝗶𝗹𝘁𝗲𝗿() → Returns elements that meet a condition. 🔹 Example: Filtering active users or removing null values. 3️⃣ 𝗿𝗲𝗱𝘂𝗰𝗲() → Reduces an array to a single value. 🔹 Example: Calculating totals, averages, or merging objects. 4️⃣ 𝘀𝗼𝗺𝗲() → Checks if at least one element matches a condition. 🔹 Example: Quick validations like “is any user logged in?”. 5️⃣ 𝗲𝘃𝗲𝗿𝘆() → Ensures all elements meet a condition. 🔹 Example: Confirm all form fields are valid before submission. 6️⃣ 𝗶𝗻𝗰𝗹𝘂𝗱𝗲𝘀() → Checks if an array contains a specific value. 🔹 Example: Searching tags or validating user roles. 7️⃣ 𝗷𝗼𝗶𝗻() → Merges elements into a single string. 🔹 Example: Creating comma-separated lists or formatted output. 8️⃣ 𝘀𝗽𝗹𝗶𝗰𝗲() → Adds or removes elements directly (mutates the array). 🔹 Example: Updating lists or dynamic UI arrays in React. 💡 𝗣𝗿𝗼 𝗧𝗶𝗽: Master these and you’ll handle 90% of real-world data transformations with ease. 💬 𝑊ℎ𝑖𝑐ℎ 𝑎𝑟𝑟𝑎𝑦 𝑚𝑒𝑡ℎ𝑜𝑑 𝑑𝑜 𝑦𝑜𝑢 𝑢𝑠𝑒 𝑡ℎ𝑒 𝑚𝑜𝑠𝑡 𝑖𝑛 𝑦𝑜𝑢𝑟 𝑝𝑟𝑜𝑗𝑒𝑐𝑡𝑠? 𝐷𝑟𝑜𝑝 𝑦𝑜𝑢𝑟 𝑎𝑛𝑠𝑤𝑒𝑟 𝑏𝑒𝑙𝑜𝑤 👇 credit- Ania Eftekhari #JavaScript #WebDevelopment #Frontend
To view or add a comment, sign in
-
🚀 𝗡𝗲𝘄 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗕𝗹𝗼𝗴 𝗗𝗿𝗼𝗽! 🔗✨ We're excited to share the next chapter in our Functional JavaScript series — “𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗖𝗼𝗺𝗽𝗼𝘀𝗶𝘁𝗶𝗼𝗻 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁: 𝗕𝘂𝗶𝗹𝗱𝗶𝗻𝗴 𝗜𝗻𝘁𝘂𝗶𝘁𝗶𝗼𝗻 𝗧𝗵𝗿𝗼𝘂𝗴𝗵 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲”, written by Anju Karanji! 💜🧠 If 𝗣𝗮𝗿𝘁 𝟭 explored how functions transform behaviour, 𝗣𝗮𝗿𝘁 𝟮 𝗴𝗼𝗲𝘀 𝗱𝗲𝗲𝗽𝗲𝗿 - into how functions can be chained, layered, and orchestrated to build elegant pipelines of logic. No theory without practice here - Anju walks step-by-step from simple function chaining → to writing your own compose() → to solving real LeetCode problems using composition to simplify complex logic. 🔂⚙️ 🔍 𝗪𝗵𝗮𝘁 𝘁𝗵𝗶𝘀 𝗽𝗮𝗿𝘁 𝗰𝗼𝘃𝗲𝗿𝘀: 🎯 What composition really means (beyond the textbook definition) 🧱 How to think in transformations rather than steps 🔄 Right-to-left evaluation & function flows 🧩 Applying composition to real algorithm challenges If you’re leveling up in 𝗥𝗲𝗮𝗰𝘁, 𝗳𝗿𝗼𝗻𝘁𝗲𝗻𝗱 𝗮𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗲, or just want cleaner, more expressive code - this is a 𝘮𝘶𝘴𝘵-𝘳𝘦𝘢𝘥. 📰 𝗥𝗲𝗮𝗱 𝘁𝗵𝗲 𝗳𝘂𝗹𝗹 𝗮𝗿𝘁𝗶𝗰𝗹𝗲: https://lnkd.in/d_diSSTA 💌 𝗦𝘂𝗯𝘀𝗰𝗿𝗶𝗯𝗲 𝗳𝗼𝗿 𝘂𝗽𝗱𝗮𝘁𝗲𝘀: https://lnkd.in/gDA5t-yP #FrontendQueens #WomenInTech #JavaScript #FunctionalProgramming #React #WebDevelopment #TechBlog ✨
To view or add a comment, sign in
-
🚀 𝗙𝗿𝗼𝗺 𝗭𝗲𝗿𝗼 𝘁𝗼 𝗛𝗲𝗿𝗼 𝗶𝗻 𝗔𝗻𝗴𝘂𝗹𝗮𝗿 — 𝗧𝗵𝗲 𝗖𝗼𝗺𝗽𝗹𝗲𝘁𝗲 𝗥𝗼𝗮𝗱𝗺𝗮𝗽 💡 🧠 𝟭. 𝗖𝗼𝗿𝗲 𝗪𝗲𝗯 & 𝗧𝘆𝗽𝗲𝗦𝗰𝗿𝗶𝗽𝘁 𝗙𝘂𝗻𝗱𝗮𝗺𝗲𝗻𝘁𝗮𝗹𝘀 Before diving into Angular, build a strong foundation: 1. HTML & CSS basics 2. JavaScript (ES6+ features: let/const, arrow functions, destructuring) 3. TypeScript (interfaces, classes, generics, decorators) 4. Modules & imports/exports 𝟮. 𝗔𝗻𝗴𝘂𝗹𝗮𝗿 𝗙𝘂𝗻𝗱𝗮𝗺𝗲𝗻𝘁𝗮𝗹𝘀 Start understanding how Angular works at its core: 1. Angular architecture (Modules, Components, Templates) 2. Angular CLI — create, serve, build 3. Components & Templates 4. Data binding (Interpolation, Property, Event, Two-way) 5. Directives (ngIf, ngFor, ngSwitch, custom directives) 6. Pipes (built-in & custom pipes) 𝟯. 𝗖𝗼𝗺𝗽𝗼𝗻𝗲𝗻𝘁 𝗜𝗻𝘁𝗲𝗿𝗮𝗰𝘁𝗶𝗼𝗻 Learn how components communicate: 1. Input() and Output() decorators 2. ViewChild and ContentChild 3. Services for shared logic 4. Event Emitters 5. Using BehaviorSubject for cross-component data sharing 𝟰. 𝗦𝗲𝗿𝘃𝗶𝗰𝗲𝘀 & 𝗗𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝗰𝘆 𝗜𝗻𝗷𝗲𝗰𝘁𝗶𝗼𝗻 Master how Angular handles logic and data: 1. Creating and injecting services 2. Hierarchical injectors 3. ProvidedIn and lifetime scopes 4. Dependency injection in components and modules 𝟱. 𝗥𝗼𝘂𝘁𝗶𝗻𝗴 & 𝗡𝗮𝘃𝗶𝗴𝗮𝘁𝗶𝗼𝗻 Handle multiple views seamlessly: 1. RouterModule and RouterLink 2. Route parameters & query params 3. Lazy loading modules 4. Route guards (AuthGuard, CanActivate, Resolve) 5. Wildcard & fallback routes 𝟲. 𝗙𝗼𝗿𝗺𝘀 𝗶𝗻 𝗔𝗻𝗴𝘂𝗹𝗮𝗿 Forms are at the heart of most applications: 1. Template-driven forms 2. Reactive forms 3. FormGroup, FormControl, FormArray 4. Validation (built-in & custom) 5. Async validators 𝟳. 𝗛𝗧𝗧𝗣 & 𝗔𝗣𝗜 𝗖𝗼𝗺𝗺𝘂𝗻𝗶𝗰𝗮𝘁𝗶𝗼𝗻 Connect your app to a backend: 1. HttpClientModule setup 2. GET, POST, PUT, DELETE requests 3. Handling Observables 4. Error handling & interceptors 5. JWT Authentication & token handling 🧠 𝟴. 𝗥𝘅𝗝𝗦 & 𝗢𝗯𝘀𝗲𝗿𝘃𝗮𝗯𝗹𝗲𝘀 Reactive programming is key in Angular: 1. What is an Observable 2. Common operators (map, filter, switchMap, mergeMap, tap) 3. Subjects & BehaviorSubjects 4. Async pipe 5. Unsubscribing best practices 𝟵. 𝗠𝗼𝗱𝘂𝗹𝗲𝘀 & 𝗔𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗲 Organize and scale your app like a pro: 1. Feature modules & shared modules 2. Core module pattern 3. Lazy loading & preloading strategies 4. Folder structure best practices 𝟭𝟬. 𝗧𝗼𝗼𝗹𝗶𝗻𝗴 & 𝗘𝗻𝘃𝗶𝗿𝗼𝗻𝗺𝗲𝗻𝘁 Improve productivity and maintainability: 1. Environments (dev, prod) 2. Angular CLI commands (ng g, ng build, ng serve) 3. ESLint / Prettier setup 4. Unit testing (Jasmine, Karma) 5. End-to-end testing (Protractor, Cypress) 𝟭𝟭. 𝗦𝘁𝗮𝘁𝗲 𝗠𝗮𝗻𝗮𝗴𝗲𝗺𝗲𝗻𝘁 Handle complex app-wide data with ease: 1. BehaviorSubject-based local state 2. NgRx / Akita / NGXS 3. Actions, Reducers, Effects 4. Store selectors and entity management
To view or add a comment, sign in
-
-
Overview of Plotly.js:: Plotly.js is a powerful, open-source JavaScript library for creating interactive data visualizations. It supports a wide range of chart types and is built on top of D3.js and stack.gl, making it suitable for both 2D and 3D graphics. Key Features:: Chart Types: Over 40 types, including: Bar Charts Line Charts Scatter Plots 3D Charts Pie Charts Statistical Graphs Maps Interactivity: Charts are interactive by default, allowing users to hover, zoom, and pan. Declarative Syntax: Charts are defined using JSON objects, making it easy to customize every aspect, such as colors and layout. Getting Started:: To use Plotly.js, include the library in your HTML: html <script src="https://lnkd.in/dHtHtxvG"></script> Then, create a simple chart with the following code: javascript const data = [{ x: ['A', 'B', 'C'], y: [10, 15, 13], type: 'bar' }]; const layout = { title: 'Sample Bar Chart' }; Plotly.newPlot('myDiv', data, layout); Performance Considerations:: Rendering: Most charts are rendered using SVG, which is compatible across browsers. For high-performance needs, especially with many data points, WebGL is used for 3D charts. Customization: Users can create custom bundles to optimize the library size based on their specific needs. Resources:: For more detailed documentation and examples, visit the official Plotly.js documentation. This resource provides comprehensive guides on how to implement various chart types and features. ghfvbj
To view or add a comment, sign in
-
🌡️ Built a real-time Temperature Converter using HTML, CSS, and JavaScript! Converts Celsius ↔ Fahrenheit instantly using pure JavaScript logic — no reloads! 🚀 Key Concepts Used: 1. DOM Manipulation 2. Event Listeners 3. Input Handling JS Code: ``` let cel = document.querySelector("#cel"); let fah = document.querySelector("#fah"); cel.addEventListener("input", () => { let celRes = (cel.value * 9) / 5 + 32; if (!Number.isInteger(celRes)) { celRes = celRes.toFixed(4); } fah.value = celRes; }); fah.addEventListener("input", () => { console.log("clicked"); let fahRes = ((fah.value - 32) * 5) / 9; if (!Number.isInteger(fahRes)) { fahRes = fahRes.toFixed(4); } cel.value = fahRes; }); cel.addEventListener("click", () => { cel.value = ""; }); fah.addEventListener("click", () => { fah.value = ""; }); ```` #JavaScript #WebDevelopment #FrontendDevelopment #Coding #LearnJavaScript #HTML #CSS #JSProjects #Programming #Developer 🎥 Watch the full demo below 👇
To view or add a comment, sign in
More from this author
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