Why & How Functions Are Objects in JavaScript 🤯 One of the most interesting (and confusing) concepts in JavaScript is that 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗮𝗿𝗲 𝗼𝗯𝗷𝗲𝗰𝘁𝘀 👉 Why? JavaScript treats functions as 𝗳𝗶𝗿𝘀𝘁-𝗰𝗹𝗮𝘀𝘀 𝗰𝗶𝘁𝗶𝘇𝗲𝗻𝘀 This means functions can: • Be stored in variables • Be passed as arguments • Be returned from other functions To support all this flexibility, JavaScript internally represents a function as a special type of object. 👉 How? When you create a function, JavaScript actually creates an 𝗼𝗯𝗷𝗲𝗰𝘁 𝗶𝗻 𝗺𝗲𝗺𝗼𝗿𝘆 with: • Executable code (the function body) • Properties (like `name` and `length`) • Methods (like `call`, `apply`, `bind`) • A hidden `[[Prototype]]` linking it to `Function.prototype` That’s why this works 👇 function greet() {} greet.language = "JavaScript"; console.log(greet.language); // JavaScript Here, we’re attaching a property to a function — something only objects can do. #JavaScript #WebDevelopment #Frontend #Programming #SoftwareEngineering #Learning
JavaScript Functions as Objects: Understanding the Concept
More Relevant Posts
-
The JavaScript Feature That Simulates Private Variables 🔒 Let’s talk about one of JavaScript’s most powerful, yet often misunderstood features: Closures. Many developers struggle with the concept initially, but once it clicks, it changes how you architect your code. At its core, a closure gives you access to an outer function’s scope from an inner function. The magic happens because the inner function "remembers" the environment in which it was created, even after the outer function has finished executing. Why is this useful? JavaScript doesn't have native "private" variables in the traditional OOP sense (though class fields are changing this). Closures allow us to emulate data privacy. Look at this classic example: creates a counter where the count variable is completely inaccessible from the outside world except through the returned increment function. function createCounter() { let count = 0; // This variable is now "private" return function() { count++; return count; }; } const counter = createCounter(); console.log(counter()); // 1 console.log(counter()); // 2 // console.log(count); // ReferenceError: count is not defined You cannot accidentally modify count from the global scope. You have created a protected state. Are you using closures intentionally in your codebase today? #JavaScript #WebDevelopment #CodingTips #SoftwareEngineering #Closures
To view or add a comment, sign in
-
-
I just published a new JavaScript article — this time on a topic that confuses almost every beginner: the Event Loop 🚀 Understanding how JavaScript handles asynchronous code separates good developers from great ones. 👉 How JavaScript Handles Async Code (Event Loop Explained Simply) https://lnkd.in/gdZcrmgM If you’re learning JS or preparing for frontend interviews, this should help clear the mystery behind async behavior 💡 Feedback and thoughts are welcome! 🙌 #JavaScript #AsyncProgramming #EventLoop #WebDevelopment #FrontendDevelopment #LearnToCode #CodingForBeginners #Programming #DevCommunity #SoftwareEngineering
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
-
✅ JavaScript Output — Let’s Understand Why This Happens This morning’s code was: let x = 0; if (x) { console.log("Inside if"); } else { console.log("Inside else"); } console.log(Boolean(x)); 💡 Correct Output Inside else false 🧠 Simple Explanation : 🔹 Step 1: Understanding the if condition In JavaScript, if does not check for true or false only. It checks whether a value is truthy or falsy. Here’s the key rule 👇 👉 0 is a falsy value in JavaScript. So when JavaScript evaluates: if (x) // x = 0 It treats 0 as false. That’s why the if block is skipped and the else block runs. ✔ Output: Inside else Step 2: Boolean(x) Now we explicitly convert x into a boolean: Boolean(0) Since 0 is falsy, it becomes: false ✔ Output: false 🎯 Key Takeaways : 0 is falsy if (x) checks truthy / falsy, not strict booleans Boolean(value) shows how JavaScript internally treats a value 📌 Common falsy values in JS: javascript Copy code false, 0, "", null, undefined, NaN 💬 Your Turn Did you already know 0 is falsy? Comment “Knew it 👍” or “Learned today 😮” #JavaScript #FrontendDevelopment #LearnJS #CodingInterview #TruthyFalsy #TechWithVeera #WebDevelopment #100DaysOfCode
To view or add a comment, sign in
-
-
JavaScript looks simple… until it doesn’t. Ever wondered why this code behaves differently in old vs modern JavaScript? if (08 == 8) { console.log("equal"); } else { console.log("unequal"); } 🥲Old JavaScript (pre-ES5) : Numbers starting with 0 were treated as octal (base-8). 08 → invalid octal Often interpreted as 0 since 8 is invalid octal number. Result: 0 == 8 // false So, Output: unequal 😄 Modern JavaScript (ES5+) Leading zero no longer means octal. 08 is treated as decimal 8 Result: 8 == 8 // true So, Output: equal 💡 The real lesson JavaScript evolved to remove confusing behavior Backward compatibility can hide dangerous bugs Always write clear, explicit code 👇Now use this 8 // decimal 0o10 // explicit octal (equals 8) Small details. Big bugs. That’s JavaScript for you. 😄 (Note - now you can't flex in front of non tech people that 018!=18 nor you can try this with Modern javascript 😑) #JavaScript #WebDevelopment #Programming #CodingTips #Developers #Tech #SoftwareEngineering LinkedIn -- Pralhad Saw
To view or add a comment, sign in
-
-
JavaScript Prototype Inheritance – Explained Simply In JavaScript, objects can learn from other objects through a mechanism known as Prototype Inheritance. When an object lacks a property or method, JavaScript looks up to its prototype to find it. Think of it like this: - A student asks a question. - If they don’t know the answer, they ask their teacher. - If the teacher doesn’t know, it goes further up. This step-by-step lookup is referred to as the Prototype Chain. Key Ideas in Simple Words: - Every JavaScript object has a hidden link called __proto__. - __proto__ points to another object (its prototype). - JavaScript searches from child → parent → Object → null. - This mechanism avoids repeating code and saves memory. - This is how JavaScript supports inheritance without classes. #JavaScript #PrototypeInheritance #JavaScriptBasics #WebDevelopment #FrontendDevelopment #LearnJavaScript #ProgrammingConcepts #CodingForBeginners #SoftwareEngineering #DeveloperCommunity
To view or add a comment, sign in
-
-
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
-
Starting JavaScript Has Been A Completely Different Experience I thought transitioning from HTML and CSS to JavaScript would be smooth. But once I started writing JS, I realized I was stepping into a new level of logic and thinking. The first time I saw scripts and brackets everywhere, it felt overwhelming. Tutorials helped a little, but I quickly learned that real understanding comes from writing code yourself… repeatedly. These first topics looked simple until I tried them: Variables: It felt strange at first — naming and storing values in code. But seeing a value appear later because I stored it earlier helped me realize how JavaScript “remembers.” Data Types: This was eye-opening. JS cares about what kind of value you're working with: number, string, Boolean, etc. It made me think more carefully about how information behaves. Operators: I thought operators were just symbols, but they control logic and calculations. One small operator can change everything the program does. And the first thing we learned to print was using console.log(). Seeing text appear in the console made me feel like the code was finally “talking back"👍. #javascript #fullstack #SoftwareEngineering.
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
-
-
🚀 JavaScript Prototype — The Hidden Power Behind Objects Most JavaScript developers use objects every day, but very few truly understand what happens behind the scenes. That’s where Prototype comes in 👇 🔹 What is Prototype? In JavaScript, every object has a hidden link to another object called its prototype. When you try to access a property or method: 1️⃣ JS first checks the object itself 2️⃣ If not found → it looks up the prototype chain This mechanism is how inheritance actually works in JavaScript. 🔹 Why Prototype Matters? ✅ Enables code reuse ✅ Saves memory (methods shared, not duplicated) ✅ Core concept behind class, extends, and this 🔹 Example (simple): function User(name) { this.name = name } User.prototype.login = function () { console.log(this.name + " logged in") } const user1 = new User("Rishu") user1.login() 💡 Even JavaScript class syntax is just syntactic sugar over prototypes. 👉 If you understand Prototype, you automatically understand: Inheritance extends & super Prototype chain How JS really works internally 📌 Tip for learners: Don’t memorize — visualize the prototype chain. If you’re learning JavaScript from basic to advanced, Prototype is a topic you must not skip. #JavaScript #Prototype #WebDevelopment #Frontend #LearningInPublic #ChaiAurCode #JSBasics #Programming
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
Amazing point 🔥✨️..