🚀 Day 4/30 – JavaScript Challenge Solved: Counter II (LeetCode 2665) Today’s problem was all about understanding closures and how functions can maintain their own state in JavaScript. What I learned: 1.How closures help preserve variable values across function calls 2.Creating multiple operations (increment, decrement, reset) using a single function 3.Clean use of arrow functions for concise code Approach: I created a function that stores the initial value and returns an object with three methods: 1.increment() -> increases value 2.decrement() -> decreases value 3.reset() -> resets to initial value All of this works because of closure, where the inner functions still remember the variable n. Key Insight: Closures are powerful when you need to encapsulate data and control how it’s modified — a very common pattern in real-world JavaScript applications. Consistency is the real game here 🔥 Let’s keep building, one day at a time. #Day4 #30DaysOfCode #JavaScript #WebDevelopment #CodingChallenge #LeetCode #Closures #LearningJourney
JavaScript Challenge: Closures and Encapsulation
More Relevant Posts
-
🚀 𝐃𝐚𝐲 𝟓/𝟏𝟓 𝐨𝐟 𝐌𝐲 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠 𝐒𝐞𝐫𝐢𝐞𝐬 Today I learned about Loops in JavaScript 🔁 👉 Loops are used to run a block of code multiple times. 📌 Types of Loops: 1️⃣ for loop for (let i = 0; i < 5; i++) { console.log(i); } 2️⃣ while loop let i = 0; while (i < 5) { console.log(i); i++; } 👉 Both loops do the same thing, but the use depends on the situation. 📌 Key Difference: for loop → when you know how many times to run while loop → when condition-based looping is needed Loops make coding faster and more efficient 💻✨ 💬 Question: Which loop do you find easier — for or while? Let’s learn together 🚀 #JavaScript #WebDevelopment #LearningInPublic #Day5 #FrontendDevelopment
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 𝟔/𝟏𝟓 𝐨𝐟 𝐌𝐲 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠 𝐒𝐞𝐫𝐢𝐞𝐬 Today I learned about Functions in JavaScript 💡 👉 Functions are reusable blocks of code They help us avoid repeating the same code again and again. 📌 Syntax: function greet() { console.log("Hello!"); } 👉 Calling the function: greet(); // Hello! 📌 Functions with parameters: function greet(name) { console.log("Hello " + name); } greet("Kanishka"); 👉 Functions make code cleaner, reusable, and easy to manage 💻✨ 💬 Question: Have you started using functions in your projects? Let’s learn together 🚀 #JavaScript #WebDevelopment #LearningInPublic #Day6 #FrontendDevelopment
To view or add a comment, sign in
-
-
🚀 Day 979 of #1000DaysOfCode ✨ 4 Useful Number Functions in JavaScript (With Cool Examples) JavaScript provides many built-in number utilities — but most developers only use a few of them. In today’s post, I’ve shared 4 super useful number functions in JavaScript along with some cool and practical examples for each. These functions can help you handle number validation, formatting, and edge cases more effectively in real-world applications. Small utilities like these might look simple, but they can save you time and help you write cleaner and more reliable logic. Once you start using them properly, you’ll notice how often they come in handy while working with data. If you work with numbers, calculations, or user inputs in JavaScript, these functions are definitely worth knowing. 👇 Which JavaScript number function do you use the most in your projects? #Day979 #learningoftheday #1000daysofcodingchallenge #FrontendDevelopment #WebDevelopment #JavaScript #React #CodingCommunity #JSDevelopers
To view or add a comment, sign in
-
🚀 I finally understood Closures in JavaScript (and it was confusing at first) At first, I thought every function call resets variables… But closures completely changed my understanding. Here’s the simple idea 👇 👉 A closure is when a function remembers variables from its outer function, even after the outer function has finished. Example: function outer() { let count = 0; return function () { count++; console.log(count); }; } const counter = outer(); counter(); // 1 counter(); // 2 counter(); // 3 💡 Why does this work? Because the inner function “remembers” the variable count. Even though outer() has already executed, the value is not lost. 🔥 Key takeaway: Normal functions → reset values every time Closures → keep values alive This concept is widely used in: ✔️ Counters ✔️ Data hiding ✔️ Event handlers Still practicing and improving my JavaScript fundamentals 💻 Have you ever struggled with closures? 🤔 #JavaScript #WebDevelopment #MERNStack #Coding #Learning #100DaysOfCode
To view or add a comment, sign in
-
-
I used to believe that JavaScript operated with some hidden “thread algorithm” behind the scenes. However, I learned that it doesn't function that way. JavaScript is single-threaded, yet it effectively manages multiple tasks simultaneously through the event loop, not threads. Here's a simplified breakdown: - There’s one main worker (the call stack). - There’s a waiting area (task queues). - There’s a loop that continuously checks what to run next. The core flow looks like this: while (true) { run sync code first if nothing is running: run all microtasks (Promises) then pick one macrotask (timers, I/O) } What surprised me the most is the priority system: Promises always execute before timers. Even a setTimeout(..., 0) has to wait its turn. As for the “threading” aspect? It exists, but not in the way you might expect. The engine (like V8) runs your code in a single thread, while the environment (browser or Node.js) utilizes multiple threads for tasks like network calls and timers. In essence, JavaScript doesn’t schedule threads; it schedules tasks. This shift in perspective can significantly change your understanding of asynchronous code. #javascript #learning #webdevelopment #programming #codewithishwar
To view or add a comment, sign in
-
Day 5 of My JavaScript Journey 🚀 Today, I learned about if/else statements and type conversion in JavaScript. The if/else statement is used to control the flow of a program based on conditions. Example: if (age > 18) { console.log("Adult"); } else { console.log("Not an adult"); } I also learned about type conversion and coercion. • Type conversion is when we manually change a value from one type to another. • Type coercion is when JavaScript automatically converts types behind the scenes. For example: "5" + 2 = "52" (coercion happens) One thing that stood out to me: JavaScript can behave unexpectedly if you don’t understand type coercion. Key takeaway: Always be mindful of data types when writing conditions and operations. #JavaScript #WebDevelopment #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
Closures looked magical when I first learned JavaScript. Now I see them as one of the most practical tools in the language. A closure is just a function carrying the variables it needs from its surrounding scope. Simple idea, huge impact. You use closures in: event handlers React hooks debounce functions middleware private state factory functions The hard part is not the definition. The hard part is knowing when that captured value becomes stale. That one mistake explains many weird bugs: "Why is this state old?" "Why did this callback run with previous data?" "Why is my timer behaving strangely?" Deep JavaScript is mostly learning where values live, how long they live, and who can still access them. #JavaScript #FrontendDevelopment #WebDevelopment
To view or add a comment, sign in
-
🚀 𝐃𝐚𝐲 𝟒 𝐨𝐟 𝐌𝐲 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠 𝐒𝐞𝐫𝐢𝐞𝐬 Today I learned about if-else (Conditions) in JavaScript 💡 👉 Conditions are used to make decisions in code. 📌 Syntax: if (condition) { // code runs if condition is true } else { // code runs if condition is false } 📌 Example: let age = 18; if (age >= 18) { console.log("You can vote"); } else { console.log("You cannot vote"); } 👉 Also learned about: else if → check multiple conditions 📌 Example: let marks = 75; if (marks > 90) { console.log("Grade A"); } else if (marks > 60) { console.log("Grade B"); } else { console.log("Grade C"); } 👉 Conditions help in building real-world logic 💻✨ 💬 Question: Have you used if-else in any project yet? Let’s learn together 🚀 #JavaScript #WebDevelopment #LearningInPublic #Day4 #FrontendDevelopment
To view or add a comment, sign in
-
-
🚀 Day 67 | JavaScript Loops & Array Iteration Today I practiced JavaScript loops and working with arrays of objects 💻 🔹 What I Worked On: • Iterated through array of objects using for loop • Printed all elements and accessed object properties like loc • Used loop with step increment (i += 2) to print alternate values • Practiced reverse counting using for and while loops • Used forEach() for cleaner array iteration 💡 Key Learning: • Arrays of objects are very common in real-world applications • Loop conditions must be handled carefully (i < length vs <= length) • forEach() is simple and readable for iteration • Multiple ways to loop → choose based on requirement 🔥 Takeaway: 👉 Mastering loops is key to handling data efficiently in JavaScript Consistency is improving logic step by step 🚀 #Day67 #JavaScript #Loops #ArrayIteration #ProblemSolving #CodingJourney #10000Coders #WebDevelopment #SravanKumarSir
To view or add a comment, sign in
-
🔍 A small JavaScript detail that can cause unexpected bugs: Object key ordering Many developers assume object keys are always returned in insertion order, but JavaScript actually follows a specific ordering rule when you iterate over object properties (Object.keys, Object.entries, for...in). The order is: • Integer index keys → sorted in ascending order • String keys → insertion order • Symbol keys → insertion order (not included in Object.keys) This is one of the reasons why using Object as a map can sometimes lead to unexpected iteration behavior when numeric keys are involved. If key order matters, Map is usually the more predictable choice since it preserves insertion order for all key types. Small language details like this are easy to overlook, but they often explain those subtle bugs you run into during debugging. #JavaScript #SoftwareEngineering #Frontend
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