✅ JavaScript Output — Why This Date Confuses Almost Everyone This morning’s code was: const date = new Date(2025, 1, 1); console.log(date.getMonth()); console.log(date.getDate()); console.log(date.getFullYear()); 💡 Correct Output 1 1 2025 Now let’s understand why this happens 👇 🧠 Simple Explanation : 🔹 How new Date(year, month, day) really works JavaScript’s Date constructor follows this rule: 👉 Month is ZERO-based That means: 0 → January 1 → February 2 → March … 11 → December So when you write: new Date(2025, 1, 1) JavaScript reads it as: 👉 1st February 2025, not January. 🔹 Line 1: date.getMonth() Since February is internally stored as 1: getMonth() → 1 ✔ Output: 1 🔹 Line 2: date.getDate() This returns the day of the month, and here it is clearly: 1 ✔ Output: 1 🔹 Line 3: date.getFullYear() Year is straightforward: 2025 ✔ Output: 2025 🎯 Key Takeaways : JavaScript months are 0-based Days and years are 1-based new Date(2025, 1, 1) = Feb 1, 2025 This causes many real-world bugs if not remembered 📌 That’s why many developers prefer libraries or careful handling when working with dates 💬 Your Turn Did you expect 1 to mean February? 😄 Comment “Tricky 😮” or “Knew this 👍” #JavaScript #FrontendDevelopment #LearnJS #CodingInterview #Dates #TechWithVeera #WebDevelopment #100DaysOfCode
JavaScript Date Confusion: Zero-Based Months
More Relevant Posts
-
What is Scope Chain in JavaScript? Understanding how JavaScript looks for variables helps everything make more sense. 🔹 Knowing why inner functions can access outer variables 🔹 Debugging undefined issues with confidence 🔹 Writing clean, predictable and bug-free JS code ❌ The code below can be confusing without understanding the Scope chain let x = 50; function outerFunction() { function innerFunction() { console.log(x); // Where does x come from? } innerFunction(); } outerFunction(); // output 50 ✅ The code below works because of the Scope Chain let x = 10; function outerFunction() { let y = 20; function innerFunction() { console.log(x, y); } innerFunction(); } outerFunction(); // output 10 20 Scope chain follow some steps that are listed below. 1️⃣ First, it looks in the current scope 2️⃣ Then, it checks the outer (parent) scope 3️⃣ This continues up to the global scope 4️⃣ If the variable is not found, JavaScript throws a ReferenceError ✅ Key takeaway: Inner functions can access variables from their outer scopes because of the scope chain. #JavaScript #ScopeChain #JSConcepts #WebDevelopment #FrontendDevelopment #LearnJavaScript #SoftwareDevelopment #DeveloperTips
To view or add a comment, sign in
-
The only JavaScript function that comes with a "Pause" button. ⏯️ In JavaScript, the golden rule of functions is usually "Run-to-Completion." Once a function starts, it doesn't stop until it returns or finishes. 𝐄𝐧𝐭𝐞𝐫 𝐭𝐡𝐞 𝐆𝐞𝐧𝐞𝐫𝐚𝐭𝐨𝐫 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧. 🤯 It completely breaks the rule. It is a special type of function that can be paused in the middle of execution and resumed later from exactly where it left off. 𝐓𝐡𝐞 𝐌𝐚𝐠𝐢𝐜 𝐒𝐲𝐧𝐭𝐚𝐱: 1️⃣ `function*`: The asterisk tells JS this isn't a normal function. 2️⃣ `yield`: This keyword acts like a pause button. It spits out a value and freezes the function's state. 3️⃣ `.next()`: This is the play button. Call it to resume the function until it hits the next `yield`. 𝐖𝐡𝐲 𝐢𝐬 𝐭𝐡𝐢𝐬 𝐮𝐬𝐞𝐟𝐮𝐥? (𝐋𝐚𝐳𝐲 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐢𝐨𝐧) Generators allow for 𝐋𝐚𝐳𝐲 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐢𝐨𝐧. You don't have to calculate a list of 1 million items at once (crashing your memory). You can generate them one by one, only when you actually need them. It’s perfect for infinite streams, ID generators, or defining complex state machines. Check out the syntax breakdown below! 👇 Have you used Generators in production (maybe with Redux Saga)? #JavaScript #WebDevelopment #CodingPatterns #AdvancedJS #SoftwareEngineering #Frontend
To view or add a comment, sign in
-
-
Today I learned about Functions in JavaScript! A Function is a reusable block of code designed to perform a specific task. It executes when it is "invoked" or called, helping developers follow the DRY (Don't Repeat Yourself) principle. There are four key types of functions in JavaScript: 1. Function Declaration These are hoisted, meaning they can be called before they are defined in the code. ex. console.log(greet("Vaseem")); function greet(name) { return `Hello, ${name}!`; } 2. Function Expression: A function assigned to a variable. Unlike declarations, these are not hoisted. ex. const add = function(a, b) { return a + b; }; 3. Arrow Functions (ES6+) A concise syntax introduced in modern JavaScript. They do not have their own this binding, making them ideal for callbacks. ex. const multiply = (x, y) => x * y; 4. Immediately Invoked Function Expression (IIFE) A function that runs as soon as it is defined. It is commonly used to create a private scope. ex. (function() { console.log("Programming started.."); })(); #JavaScript #WebDevelopment #Programming #CodingTips #SoftwareEngineering #Frontend #JSFunctions #TechLearning
To view or add a comment, sign in
-
Treat your functions like VIPs. That's the secret to JavaScript's power. 🎩✨ In many older languages, functions are just specific blocks of code. In JavaScript, they are 𝐅𝐢𝐫𝐬𝐭-𝐂𝐥𝐚𝐬𝐬 𝐂𝐢𝐭𝐢𝐳𝐞𝐧𝐬. But what does that actually mean? And how is it different from a 𝐇𝐢𝐠𝐡𝐞𝐫-𝐎𝐫𝐝𝐞𝐫 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧? Here is the breakdown: 🥇 𝐅𝐢𝐫𝐬𝐭-𝐂𝐥𝐚𝐬𝐬 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬 (𝐓𝐡𝐞 𝐂𝐚𝐩𝐚𝐛𝐢𝐥𝐢𝐭𝐲) This refers to the 𝑙𝑎𝑛𝑔𝑢𝑎𝑔𝑒 𝑓𝑒𝑎𝑡𝑢𝑟𝑒 itself. It means JavaScript treats functions just like any other variable (like a number or string). • ✅ You can assign them to variables. • ✅ You can pass them as arguments to other functions. • ✅ You can return them from other functions. 🚀 𝐇𝐢𝐠𝐡𝐞𝐫-𝐎𝐫𝐝𝐞𝐫 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬 (𝐓𝐡𝐞 𝐈𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭𝐚𝐭𝐢𝐨𝐧) This is a function that 𝑢𝑡𝑖𝑙𝑖𝑧𝑒𝑠 that First-Class capability. If a function accepts another function as a parameter (like a callback) or returns a function (like a factory), it is a 𝐇𝐢𝐠𝐡𝐞𝐫-𝐎𝐫𝐝𝐞𝐫 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧. 𝐑𝐞𝐚𝐥-𝐖𝐨𝐫𝐥𝐝 𝐄𝐱𝐚𝐦𝐩𝐥𝐞: The `multiplier` function in the infographic is a classic example of a Higher-Order function returning a First-Class function. This pattern is the basis of 𝐂𝐥𝐨𝐬𝐮𝐫𝐞𝐬 and 𝐂𝐮𝐫𝐫𝐲𝐢𝐧𝐠! Check out the visual guide below to master these patterns. 👇 What is your favorite Higher-Order function? (I'm a big fan of `.reduce()`) #JavaScript #FunctionalProgramming #WebDevelopment #CodingPatterns #SoftwareEngineering #Frontend
To view or add a comment, sign in
-
-
JavaScript Scope Chain — Explained Simply (No Fluff) If you’ve ever wondered “Where the hell did this variable come from?”, this is for you. Understanding the scope chain explains: Why inner functions can access outer variables Why undefined or ReferenceError happens How JavaScript actually resolves variables ❌ Confusing without scope chain let x = 50; function outerFunction() { function innerFunction() { console.log(x); // Where does x come from? } innerFunction(); } outerFunction(); // 50 ✅ Clear when you know the rule let x = 10; function outerFunction() { let y = 20; function innerFunction() { console.log(x, y); } innerFunction(); } outerFunction(); // 10 20 🔍 How JavaScript finds variables 1️⃣ Look in the current scope 2️⃣ Move to the parent scope 3️⃣ Continue up to the global scope 4️⃣ Not found? → ReferenceError Key takeaway: Inner functions don’t magically get variables — JavaScript walks up the scope chain until it finds them. If you don’t understand scope, you’ll write unpredictable JS. If you do, debugging becomes boring — and that’s a good thing. #JavaScript #WebDevelopment #Frontend #ReactJS #Programming #ScopeChain #CleanCode
To view or add a comment, sign in
-
Day 3: JavaScript Hoisting — Magic or Logic? Yesterday, we learned that JavaScript creates memory for variables before it executes the code. Today, let’s see the most famous (and sometimes confusing) result of that: Hoisting. What is Hoisting? Hoisting is a behavior where you can access variables and functions even before they are initialized in your code without getting an error. Wait... why didn't it crash? 🤔 If you try this in other languages, it might throw an error. But in JS: During Memory Phase: JS saw var x and gave it the value undefined. It saw the function getName and stored its entire code. During Execution Phase: When it hits console.log(x), it looks at memory and finds undefined. When it hits getName(), it finds the function and runs it! ⚠️ The "Let & Const" Trap Does hoisting work for let and const? Yes, but with a catch! They are hoisted, but they are kept in a "Temporal Dead Zone". If you try to access them before the line where they are defined, JS will throw a ReferenceError. Pro Tip: Always define your variables at the top of your scope to avoid "Hoisting bugs," even though JS "magically" handles them for you. Crucial Takeaway: Hoisting isn't code physically moving to the top; it’s just the JS Engine reading your "ingredients" before "cooking" (Phase 1 vs Phase 2). Did you know about the Temporal Dead Zone? Let me know in the comments! #JavaScript #WebDev #100DaysOfCode #Hoisting #CodingTips #FrontendDeveloper
To view or add a comment, sign in
-
-
🚨 Still struggling with JavaScript Functions? Choosing the right function style can make your code look like a pro’s or a total mess. Let’s break down the 3 most common types so you never get confused again. 👇 🔵 Function Declaration (The Classic) function greet(name) { return `Hello, ${name}!`; } - Traditional way of writing functions. - Hoisted: You can call it even before you define it in your code. - Best for general-purpose utility functions. 🟢 Arrow Function (The Modern Standard) const greet = (name) => `Hello, ${name}!`; - Concise syntax: Saves lines of code. - Implicit Return: If it's one line, you don't even need the return keyword! - this binding: Does not have its own this (perfect for callbacks and React). 🟡 Return Functions (The Logic Powerhouse) function add(a, b) { return a + b; // Stops execution and sends a value back } - The "Output" tool: Use return when you need the function to give you a result to use later. - Pro Tip: Anything written after a return statement inside a function will never run! ✅ When to use what? - Use Arrow Functions for almost everything in modern projects (cleaner & better for scope). - Use Function Declarations if you need "hoisting" or want a more descriptive, traditional structure. - Always use return if your function is meant to calculate or "get" a value for another part of your code. Summary: Stop writing long functions when an arrow function can do it in one line! 🚀 👉 Follow Rahul R. Patil for more clear and simple JavaScript tips! #JavaScript #WebDevelopment #CodingTips #SoftwareEngineering #RahulPatil
To view or add a comment, sign in
-
-
JavaScript feels simple… until someone asks: 𝐂𝐚𝐧 𝐲𝐨𝐮 𝐞𝐱𝐩𝐥𝐚𝐢𝐧 𝐡𝐨𝐰 𝐭𝐡𝐢𝐬 𝐜𝐨𝐝𝐞 𝐫𝐮𝐧𝐬 𝐢𝐧𝐭𝐞𝐫𝐧𝐚𝐥𝐥𝐲? Initially, JavaScript felt magical to me. Hoisting. Call stack. Async. I was just memorizing rules, not understanding what was really happening. Then I learned the Execution Context + Call Stack mental model (thanks to Akshay Saini 🚀 ) — and suddenly, everything clicked. 𝐇𝐨𝐰 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐚𝐜𝐭𝐮𝐚𝐥𝐥𝐲 𝐫𝐮𝐧𝐬: JavaScript runs inside an Execution Context. It starts with the 𝐆𝐥𝐨𝐛𝐚𝐥 𝐄𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 𝐂𝐨𝐧𝐭𝐞𝐱𝐭. It’s just an environment where your code is prepared and then run. Each execution context has two phases: 𝐌𝐞𝐦𝐨𝐫𝐲 𝐩𝐡𝐚𝐬𝐞 • Memory is allocated to variables and functions • Variables get a placeholder value: undefined • Functions are stored completely 𝐄𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 𝐩𝐡𝐚𝐬𝐞 • Code runs line by line • Values are assigned to variables • When a function is called, a new execution context is created • When the function finishes, its context is removed 𝐖𝐡𝐨 𝐦𝐚𝐧𝐚𝐠𝐞𝐬 𝐚𝐥𝐥 𝐭𝐡𝐢𝐬? The Call Stack • Global Execution Context goes first • Each function call goes on top • When finished → it gets popped off Credits to Akshay Saini 🚀 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐃𝐞𝐞𝐩 𝐃𝐢𝐯𝐞 𝐩𝐥𝐚𝐲𝐥𝐢𝐬𝐭: https://lnkd.in/gy5ypSGf Blog: https://lnkd.in/gd8ZFDiJ #javascript #webdevelopment #interviews #engineering #learning #namastejavascript #namastejs #namastedev #akshaysaini #nodejs #systemdesign
To view or add a comment, sign in
-
-
🔑 Variables & Scope — The Foundation of Writing Smarter JavaScript One thing that has really clicked for me recently in JavaScript is how powerful variables and scope actually are. At first, they look simple: * var * let * const But when you truly understand how they work together with scope, your code becomes cleaner, safer, and way less confusing. Here’s what I learned: 👉 var: can be redeclared and reassigned (but can cause unexpected bugs if misused) 👉 let: can be reassigned but not redeclared (great for controlled updates) 👉 const: is constant — once assigned, it cannot change (perfect for fixed values) But the real magic is scope. Scope determines where your variable lives and who can “see” it: * Global scope – accessible everywhere * Function scope – available only inside a function * Block scope– { } creates its own world for let and const This is why a variable may “mysteriously” work in one place and break in another. It’s not JavaScript being wicked — it’s scope doing its job😄 Understanding variables & scope saves you from: ✔️ accidental overwriting ✔️ hard-to-track bugs ✔️ unpredictable behavior And here’s the bigger life lesson 👇 Just like variables have scope, our attention should too. Focus on what matters now, avoid distractions outside your “block,” and your productivity skyrockets. I’m excited to keep going deeper into JavaScript, one core concept at a time. 🚀 💬 Your turn: Which did you struggle with first — var, let, const, or scope? #JavaScript #WebDevelopment #CodingJourney #LearnInPublic #ProgrammingBasics
To view or add a comment, sign in
-
-
💻 Day 17 of #100DaysOfCode — 5 Useful JavaScript Tips You’ll Actually Use 🚀 I’m on Day 16 of my coding journey, and today I’m sharing a few JavaScript tips that make coding smoother, cleaner, and a bit more fun 👇 🧠 1️⃣ Use ?? Instead of || const name = user.name ?? "Guest"; ✅ ?? only falls back if the value is null or undefined, not for 0 or "". ⚡ 2️⃣ Optional Chaining to Avoid Errors console.log(user?.profile?.email); ✅ Prevents “undefined” errors when accessing nested data. 🍀 3️⃣ Destructure for Cleaner Code const { name, age } = user; const [first, second] = items; ✅ Reduces repetition and keeps code simple. 🔥 4️⃣ Quick Boolean Conversion const isValid = !!value; ✅ Fast way to check if a value is truthy or falsy. 💡 5️⃣ Ternary Operator for Simple Conditions const message = isLoggedIn ? "Welcome back!" : "Please log in."; ✅ Keeps your logic concise and readable. Every day I code, I learn something new — not just about JavaScript, but about how small improvements compound into real growth 💪 Which of these JS tips do you use most often? 👇 #Day16 #100DaysOfCode #JavaScript #CodingTips #WebDevelopment #Frontend #LearnInPublic #Consistency
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