𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐭𝐫𝐞𝐚𝐭𝐬 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬 𝐚𝐬 𝐯𝐚𝐥𝐮𝐞𝐬 — 𝐚𝐧𝐝 𝐭𝐡𝐚𝐭 𝐜𝐡𝐚𝐧𝐠𝐞𝐬 𝐡𝐨𝐰 𝐰𝐞 𝐰𝐫𝐢𝐭𝐞 𝐩𝐫𝐨𝐠𝐫𝐚𝐦𝐬. While revisiting some fundamentals today, I explored how 𝐟𝐢𝐫𝐬𝐭-𝐜𝐥𝐚𝐬𝐬 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬 shape the way JavaScript handles behavior and logic. In JavaScript, functions are not just blocks of code. They can be 𝐚𝐬𝐬𝐢𝐠𝐧𝐞𝐝 𝐭𝐨 𝐯𝐚𝐫𝐢𝐚𝐛𝐥𝐞𝐬, 𝐩𝐚𝐬𝐬𝐞𝐝 𝐚𝐬 𝐚𝐫𝐠𝐮𝐦𝐞𝐧𝐭𝐬, and even 𝐫𝐞𝐭𝐮𝐫𝐧𝐞𝐝 𝐟𝐫𝐨𝐦 𝐨𝐭𝐡𝐞𝐫 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬. This ability is what makes functions first-class citizens in the language. This also led me to understand the difference between 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧 𝐝𝐞𝐜𝐥𝐚𝐫𝐚𝐭𝐢𝐨𝐧𝐬 and 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧 𝐞𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧𝐬. A function declaration is fully hoisted, meaning the function can be invoked even before it appears in the code. But with a function expression, only the variable is hoisted — not the function value itself. Until the assignment happens, the variable holds undefined, which is why calling it early leads to a 𝐓𝐲𝐩𝐞𝐄𝐫𝐫𝐨𝐫. Another interesting concept is 𝐚𝐧𝐨𝐧𝐲𝐦𝐨𝐮𝐬 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬. These are functions𝐰𝐢𝐭𝐡𝐨𝐮𝐭 𝐚 𝐧𝐚𝐦𝐞, typically used when functions are treated as values. When a function expression includes a name, it becomes a 𝐧𝐚𝐦𝐞𝐝 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧 𝐞𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧, where the name is accessible only inside the function body. Finally, revisiting 𝐩𝐚𝐫𝐚𝐦𝐞𝐭𝐞𝐫𝐬 𝐯𝐬 𝐚𝐫𝐠𝐮𝐦𝐞𝐧𝐭𝐬 clarified how parameters act as 𝐥𝐨𝐜𝐚𝐥 𝐯𝐚𝐫𝐢𝐚𝐛𝐥𝐞𝐬 𝐢𝐧𝐬𝐢𝐝𝐞 𝐚 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧 𝐬𝐜𝐨𝐩𝐞, receiving the values passed during invocation. Understanding these fundamentals makes it much easier to reason about patterns like callbacks, closures, and asynchronous JavaScript. #JavaScript #SoftwareEngineering #DeveloperJourney #LearningInPublic
JavaScript Functions as Values and Variables
More Relevant Posts
-
New Blog Published: Mastering call(), apply(), and bind() in JavaScript Ever wondered why the this keyword in JavaScript sometimes behaves unexpectedly? Many developers memorize call(), apply(), and bind() but don’t fully understand why these methods exist or when to actually use them. In real-world JavaScript applications, controlling this is important for writing reusable functions, borrowing methods between objects, and fixing context issues in callbacks. In this blog, I break down: • Why JavaScript needed call(), apply(), and bind() • The core difference between these three methods • When to use each one in real scenarios • A simple restaurant chef analogy to make the concept intuitive • Practical code examples to make everything clear Written in a simple and practical way for developers who want to truly understand how this works in JavaScript. 🔗 Read here: https://lnkd.in/g6-xTkcS Hitesh Choudhary Piyush Garg Akash Kadlag Jay Kadlag Anirudh Sir #JavaScript #SoftwareDevelopment #Programming #WebDev #ChaiCode
To view or add a comment, sign in
-
🚀 JavaScript Fundamentals Series — Part 4 Functions are where JavaScript becomes powerful and reusable. Instead of repeating code, functions let you create reusable logic blocks. In this guide you'll learn: • What functions actually are • Parameters vs arguments • Return values • Function declarations vs expressions • How functions organize your code Functions are the foundation of almost everything in JavaScript. Full guide 👇 https://lnkd.in/dtgFaW2r #javascript #webdevelopment #coding
To view or add a comment, sign in
-
Understanding Currying in JavaScript Ever heard of currying? It's a cool technique that makes your code cleaner and more reusable! What is Currying? Currying is when you break down a function that takes multiple arguments into a series of functions that each take one argument. Simple Example: Instead of: javascript function add(a, b, c) { return a + b + c; } add(1, 2, 3); // Output: 6 You write: javascript function add(a) { return function(b) { return function(c) { return a + b + c; } } } add(1)(2)(3); // Output: 6 Why Use It? Reusability: Create specialized functions easily Cleaner code: Break complex logic into smaller pieces Function composition: Combine functions in powerful ways Real-world benefit: javascript const addFive = add(5); addFive(2)(3); // Output: 10 Now you have a function that always adds 5 first! #javascript #webDeveloper #uiDeveloper #frontendDeveloper #react #reactjs
To view or add a comment, sign in
-
Just published a new blog on JavaScript Execution Context, Hoisting, TDZ, and Call Stack. While learning these concepts, I realized how important it is to understand how JavaScript actually executes code behind the scenes. It helps explain a lot of behaviors that otherwise feel confusing. In this blog, I’ve tried to break down these concepts simply and clearly. If you’re learning JavaScript or revisiting the fundamentals, feel free to check it out: https://lnkd.in/d6S5HGPy Open to feedback and suggestions. #JavaScript #LearningInPublic #WebDevelopment #BuildInPublic #ChaiCode #Hoisting
To view or add a comment, sign in
-
🚀 Just published a new blog on Async/Await in JavaScript: Writing Cleaner Asynchronous Code. In this article, I explain how async/await helps handle asynchronous operations in a cleaner and more readable way compared to callbacks and promises. I covered how async functions work, how to use await, and simple examples to understand the flow step by step. 📖 Read the full article here: https://lnkd.in/gTbGmXPQ Inspired by the amazing teaching of Hitesh Choudhary Sir and Piyush Garg Sir from Chai Aur Code. ☕💻 #javascript #webdevelopment #learninginpublic #chaiAurCode
To view or add a comment, sign in
-
🚀 Just published a new blog on The Magic of this, call(), apply(), and bind() in JavaScript. In this article, I explain the concept of this in JavaScript with simple examples—how it works inside functions and objects, and how call(), apply(), and bind() help control the function’s context. I also included a clear comparison to understand the difference between them. 📖 Read the full article here: https://lnkd.in/gwCVf4pj Inspired by the amazing teaching of Hitesh Choudhary Sir and Piyush Garg Sir from Chai Aur Code. ☕💻 #javascript #webdevelopment #learninginpublic #chaiAurCode
To view or add a comment, sign in
-
𝗧𝗵𝗲 𝗕𝗮𝘀𝗶𝗰𝘀 𝗢𝗳 𝗔𝗿𝗿𝗼𝗪 𝗙𝘂𝗻𝗰𝗍𝗶𝗼𝗻𝘀 You want to write functions in JavaScript with less code. Arrow functions are a shorter way to write functions. They were introduced in ES6 and have become popular because they: - Reduce the amount of code you write - Make your code cleaner and easier to read - Have special features Think of arrow functions as a simpler way to write functions. They get you from point A to point B faster. Here's how you normally write functions: function greet(name) { returnHello + name } const greet = (name) => { returnHello + name } The arrow function: - Removes the function keyword - Adds an arrow => after the parameters - Stores the function in a variable When your arrow function has one parameter, you can remove the parentheses. const double = num => num * 2 Both work exactly the same. When you have multiple parameters, you must use parentheses. const add = (x, y) => x + y Arrow functions can return implicitly when they have one expression. const add = (x, y) => x + y Use explicit return when your function has multiple lines of code. Use implicit return when your function is one line that returns a value. Arrow functions are perfect for: - Short, simple functions - Callback functions - Cleaner, more readable code They reduce boilerplate code and make your JavaScript more modern and readable. Source: https://lnkd.in/gM5Va8nP
To view or add a comment, sign in
-
𝐋𝐞𝐭'𝐬 𝐞𝐱𝐩𝐥𝐨𝐫𝐞 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐚 𝐛𝐢𝐭😉 This question caught my attention with how simple it is, based on inference from my experience with JavaScript, and I thought to explain 𝗜𝘀 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗮 𝗱𝘆𝗻𝗮𝗺𝗶𝗰𝗮𝗹𝗹𝘆 𝘁𝘆𝗽𝗲𝗱 𝗼𝗿 𝘀𝘁𝗮𝘁𝗶𝗰𝗮𝗹𝗹𝘆 𝘁𝘆𝗽𝗲𝗱 𝗹𝗮𝗻𝗴𝘂𝗮𝗴𝗲? 𝐓𝐡𝐞 𝐚𝐧𝐬𝐰𝐞𝐫: JavaScript is dynamically typed. In dynamically typed languages, all type checks are performed at runtime, that is, when the program is executing. So this means you can assign anything to the variable and it will work. This is because types are associated with values, not variables. This flexibility is one of the reasons JavaScript became so popular. It allows developers to move quickly and write readable code. But it also introduces trade-offs. (Nothing really is for free😔) Because types are checked only at execution, certain mistakes only appear when the code runs. Some of which include: ☑ Unexpected type coercion ☑ Runtime errors ☑ Harder-to-maintain large codebases This is one of the reasons TypeScript gained popularity. Unlike JavaScript, TypeScript, a statically typed language, insists on all checks being performed during compile/build run before we execute our program. This allows developers to catch many type-related errors before the code runs. In the end, understanding JavaScript’s dynamic nature helps you: ☑ Write safer code ☑ Avoid subtle bugs ☑ Appreciate when tools like TypeScript are helpful #JavaScript #TypeScript #WebDevelopment #SoftwareEngineering #FrontendDevelopment
To view or add a comment, sign in
-
🚀 New Blog Published! I’ve written a new article on Arrow Functions in JavaScript as part of my Web Dev Cohort 2026 learning journey. In this blog, I explained: • What Arrow Functions are • Arrow Function syntax • Implicit vs Explicit return • Difference between normal functions and arrow functions • Simple examples using map() If you're learning JavaScript, this guide will help you understand arrow functions easily. 📖 Read the full blog here: https://lnkd.in/gkETTsJk #JavaScript #WebDevelopment #LearningInPublic #WebDevCohort #100DaysOfCode #arrowFunction
To view or add a comment, sign in
-
🚨 Ever wondered what actually happens behind the scenes when your JavaScript code runs? Or why some variables behave like they exist before you even write them? 🤯 Let’s break it down — because understanding this changes how you write JavaScript forever. 👇 💡 Execution Context in JavaScript Every time your JS code runs, it goes through two powerful phases: 🔹 1. Memory Creation Phase (a.k.a Variable Environment) • JavaScript scans your code before execution • Allocates memory for variables and functions • Variables are initialized with undefined • Functions are stored with their entire definition 👉 This is why hoisting happens! 🔹 2. Code Execution Phase (Thread of Execution) • Code runs line by line • Variables get their actual values assigned • Functions are invoked and executed • A single thread handles everything (yes, JS is single-threaded!) ⚡ Important Insight JavaScript doesn’t just “run code” — it prepares and then executes. That preparation step is what often confuses developers. 😬 Common Confusion • Accessing variables before assignment → undefined • Thinking JS executes top-to-bottom only → not entirely true • Misunderstanding hoisting → leads to bugs 🔥 Pro Tip Mastering execution context helps you: • Debug faster • Write predictable code • Truly understand closures, scope, and async behavior 📌 Think of it like this: Memory Creation = Setting the stage 🎭 Execution Phase = Performing the play 🎬 Once you see it this way, JavaScript starts making a lot more sense. #JavaScript #WebDevelopment #AsyncProgramming #Coding #LearnToCode #100DaysOfCode
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