The JavaScript filter() method is a powerful way to create a new array containing only the elements that meet a certain condition. It goes through each item in the array and returns the ones that pass the test you provide. For example, to get all even numbers from an array: javascript const numbers = [1, 2, 3, 4, 5]; const evens = numbers.filter(num => num % 2 === 0); console.log(evens); // Output: [2, 4] Use filter() whenever you need to sift through data and extract only what’s relevant, making your code more declarative and concise! #JavaScript #WebDevelopment #CodingTips #ArrayMethods
More Relevant Posts
-
🚀 JavaScript Trick of the Day! const a = { valueOf: () => 2 * 1 }; console.log(2 + a); // What’s the output? 💡 Answer: 4 Why? When using +, JavaScript tries to convert objects to a primitive. Since the object has a custom valueOf(), JS uses that → returns 2. So: 2 + 2 = 4. 🧠 A reminder that JavaScript type coercion can be surprising — but powerful when understood! #JavaScript #WebDevelopment #CodingTips #100DaysOfCode #JSInterviewPrep
To view or add a comment, sign in
-
JavaScript for 15 Days – Day 6: If / Else Statements. Today you'll know how to make your JavaScript code think and decide using if, else if, and else statements. These structures allow programs to run different blocks of code based on specific conditions just like real-life decisions: let score = 85; if (score >= 90) { console.log("Excellent!"); } else if (score >= 70) { console.log("Good job!"); } else { console.log("Keep practicing!"); } Key Takeaways: if checks the first condition. else if tests another one if the first fails. else runs only when all above are false. Conditional statements are what make code dynamic and smart they let programs respond to different situations! #JavaScript #FrontendDevelopment #WebDevelopment #CodingJourney #LearnToCode #15DaysJS #DevPerDay
To view or add a comment, sign in
-
𝐈 𝐛𝐞𝐭 𝐲𝐨𝐮 𝐝𝐢𝐝𝐧’𝐭 𝐤𝐧𝐨𝐰 𝐭𝐡𝐢𝐬… . . . There’s a 30-𝐲𝐞𝐚𝐫-𝐨𝐥𝐝 𝐛𝐮𝐠 in JavaScript — and it’ll never be fixed. 𝐭𝐲𝐩𝐞𝐨𝐟 𝐧𝐮𝐥𝐥 === "𝐨𝐛𝐣𝐞𝐜𝐭"; // 𝐭𝐫𝐮𝐞 🤯 This is not a quirk — it’s a mistake. When JS was created in 1995, 𝐧𝐮𝐥𝐥 was stored as all 0’s in memory. Objects also used 0 as a type tag. So JS confused them. Now fixing it would break the web. 💀 #JavaScript #ProgrammingHistory #WebDev #JavaScript #WebDevelopment #ProgrammingHistory
To view or add a comment, sign in
-
🚀 JavaScript Trick You Should Know! What do you think the result will be? 👇 "5" + 2 // ? "5" - 2 // ? ✅ "5" + 2 → "52" ✅ "5" - 2 → 3 Why? Because (+) combines strings, while (-) forces JavaScript to convert "5" into a number. A small example — but it shows how type coercion can completely change your output. ⚡ #JavaScript #FrontendDevelopment #CodingTips
To view or add a comment, sign in
-
🗓️ Day 12 of JavaScript LeetCode Challenge Problem: 2619. Array Prototype Last Approach: In this problem, I enhanced the Array prototype to include a custom method last() that returns the last element of an array. If the array is empty, it returns -1. I used the this keyword in JavaScript, which refers to the current object on which the function is being called. In global scope, this refers to the window object. Inside a function or method, the value of this depends on how the function is called. Here, since the last() method is called on an array, this refers to that specific array instance. Using this reference, we can easily access its length and return the last element.
To view or add a comment, sign in
-
-
#Day2Nov #dailylearning 💡 What is a Closure in JavaScript? A closure is created when a function remembers and accesses variables from its outer function — even after the outer function has finished executing. In simple words, 👉 A closure lets a function use values that were in scope when it was created. 🧠 Example: function outer() { let count = 0; function inner() { count++; console.log(count); } return inner; } const counter = outer(); counter(); // 1 counter(); // 2 Here, inner() still remembers the variable count from outer() — that’s a closure! 🔍 Why Closures are Useful: To remember data between function calls To create private variables Used in data hiding and function factories #masiverse #WebDevelopment #Javascript #learning
To view or add a comment, sign in
-
Exploring Different Methods to Calculate the Sum of Two Numbers in JavaScript When it comes to adding two numbers in JavaScript, there are various approaches to achieve the result. One common method involves defining variables for the numbers, such as: var a = 2; var b = 3; function sum(){ return a + b; } sum(); Alternatively, you can directly pass the numbers into a function for instant calculation: function sum(a, b){ return a + b; } sum(2, 3); For real-time feedback while coding, utilizing "console.log" can display the result promptly: function sum(a, b){ console.log(a + b); } sum(2, 3); Remember, JavaScript's case sensitivity emphasizes the importance of accurately typing uppercase and lowercase letters in your code. Stay precise for seamless execution! #JavaScriptTips #CodingMethods #JavaScript
To view or add a comment, sign in
-
#javascript #javascriptTips Converting input type number by using parseInt or Number() function or using a plus operator for instance const num = +value. We can instead use the valueAsNumber property which gives the value as number type simple 🪄 Picture Credit: Steve Sewell 🙌
To view or add a comment, sign in
-
-
🚀 JavaScript Hoisting Explained (Simply!) Hoisting means JavaScript moves all variable and function declarations to the top of their scope before code execution. If that definition sounds confusing, see this example 👇 console.log(a); var a = 5; Internally, JavaScript actually does this 👇 var a; // declaration is hoisted (moved up) console.log(a); a = 5; // initialization stays in place ✅ Output: undefined --- 🧠 In Short: > Hoisting = JS reads your code twice: 1️⃣ First, to register variables & functions 2️⃣ Then, to execute the code line by line --- 💡 Tip: var → hoisted & initialized as undefined let / const → hoisted but not initialized (stay in Temporal Dead Zone) --- #JavaScript #Hoisting #WebDevelopment #CodingTips #JSInterview #Frontend #React #100DaysOfCode
To view or add a comment, sign in
-
Today is Day 10, and I explored some of the most important function-related concepts in JavaScript: ✅ Pure Functions ✅ First-Class & Higher-Order Functions ✅ map(), filter(), and reduce() ✅ Argument Object & Rest Parameter ✅ Variable Scope (Global, Local, Block) ✅ Scope & Scope Chain ✅ Recursion & Closures #Day10 #JavaScript #FrontendDevelopment #100DaysOfCode #LearningInPublic
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
Perfect