"use strict" — tiny directive, big impact. 🚀 If you’ve written JS for a while you’ve probably seen: "use strict"; Placed at the top of a file or function, it flips JavaScript into a stricter mode that helps catch mistakes early and avoids some weird legacy behaviors. Why use it? ✅ Turns silent errors into thrown errors (helps you catch bugs sooner). ✅ Prevents accidental globals — assignment to an undeclared variable throws. ✅ Disallows duplicate parameter names and deprecated features (like with). ✅ Makes optimizations more predictable for engines. Bonus: ES modules (import/export) are strict by default — no need to add it there. Quick example: // Non-strict — creates a global by accident function bad() { x = 10; } bad(); // x is now global // Strict — throws, so you fix it function good() { "use strict"; x = 10; // ReferenceError: x is not defined } Best practice: Prefer ES modules (they’re strict by default). If using scripts, put "use strict"; at the top of the file. Don’t assume it prevents all unsafe behavior — it helps, but write clear, tested code. Have you run into a bug that "use strict" would’ve caught? Share below — always fun to learn from real mistakes. 👇 #javascript #webdev #frontend #bestpractices
"use strict" directive explained for JavaScript
More Relevant Posts
-
🚀 ... — The Tiny Operator That Does Double Duty! Rest & Spread In JavaScript, a single operator ... serves two powerful roles based on where it’s used 👇 If it’s on the Left — it Gathers. Else, on the Right — it Scatters. 🔹 Spread Operator ➡ Purpose: Expands (or spreads) elements of an array or object into individual items. ➡ Used on the right side of =. const arr1 = [10, 20, 30]; const arr2 = [...arr1, 40, 50]; // arr2 → [10, 20, 30, 40, 50] 🔹 Rest Operator ➡ Purpose: Collects (or bundles) remaining elements into a single variable. ➡ Used on the left side of = during destructuring. const arr = [10, 20, 30, 40]; const [a, b, ...rest] = arr; // a = 10, b = 20, rest = [30, 40] 💭 Quick Tip: ... on the right → Spread ... on the left → Rest Both simplify working with collections and make code cleaner and more readable ✨ #JavaScript #WebDevelopment #CodingTips #JSOperators #SpreadOperator #RestOperator #LearnJavaScript #FrontendDevelopment #WebDevCommunity #TechLearning
To view or add a comment, sign in
-
🧩 Undefined vs Null 🤔 ✨ When there’s no existence, it’s Undefined, whereas emptiness exists by choice, it’s Null. 🔹 undefined → When a variable is declared but not assigned by the user, JavaScript itself assigns the value undefined by default. let a; console.log(a); // undefined 🧠 It means: “No value exists yet — JavaScript couldn’t find one.” 🔹 null → When a variable is explicitly assigned by the user to represent emptiness, it holds the value null. let b = null; console.log(b); // null 💡 It means: “The developer intentionally set this to nothing.” ⚙️ Type check curiosity typeof null; // "object" ❗ (a known JavaScript bug) typeof undefined; // "undefined" 🚫 Falsy values Both are falsy during condition checks 👇 if (!undefined && !null) console.log('Falsy values!'); 🎯 In short: 🟠 undefined → Not assigned by the user → JavaScript assigns it automatically. 🟣 null → Explicitly assigned by the user → JavaScript doesn’t assign it. 🔖 Hashtags #JavaScript #WebDevelopment #Frontend #CodingTips #LearnToCode #JSBasics #WebDevCommunity #JavaScriptTips #CodeNewbie #DeveloperInsights
To view or add a comment, sign in
-
🚨 Understanding JavaScript Errors — A Developer’s Everyday Companion As JavaScript developers, we’ve all seen that scary red text in the console 😅 However, understanding why an error occurs is the key to writing cleaner, more predictable code. Here are the main types of JavaScript errors every developer should know 👇 🧩 1️⃣ Syntax Error Occurs when the code violates JavaScript syntax rules. Example: console.log("Hello World" (Missing closing parenthesis) 🔍 2️⃣ Reference Error Happens when trying to use a variable that doesn’t exist. Example: console.log(userName); // userName is not defined ⚙️ 3️⃣ Type Error Thrown when a value is of the wrong type. Example: let num = 5; num.toUpperCase(); // ❌ num is not a string 🚫 4️⃣ Range Error Occurs when a number or value is outside its allowed range. Example: let arr = new Array(-5); // ❌ invalid array length ⚡ 5️⃣ Eval Error Related to the eval() function (rarely used today — and not recommended). 💡 Pro Tip: Always handle errors gracefully using try...catch blocks, and make logging your best friend during debugging. Errors are not enemies — they’re feedback from the JavaScript engine helping us write better code. 💪 #JavaScript #WebDevelopment #Frontend #Programming #ErrorHandling #Debugging #DeveloperCommunity
To view or add a comment, sign in
-
🤫 𝐇𝐢𝐝𝐝𝐞𝐧 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐄𝐫𝐫𝐨𝐫𝐬 𝐓𝐡𝐚𝐭 𝐆𝐨 𝐔𝐧𝐧𝐨𝐭𝐢𝐜𝐞𝐝 🕵️♂️ Have you ever written code that looks perfect — no errors, no warnings — yet it still doesn’t work? That’s the tricky world of JavaScript Silent Errors! 😅 --- 💡 𝑾𝒉𝒂𝒕 𝑨𝒓𝒆 𝑺𝒊𝒍𝒆𝒏𝒕 𝑬𝒓𝒓𝒐𝒓𝒔? Silent errors are mistakes in your code that don’t trigger visible error messages. Your program continues running, but it doesn’t behave as expected. Example 👇 "use strict"; x = 10; // ❌ ReferenceError in strict mode Without "use strict", this line creates a global variable silently — no errors, but trouble later! 😬 --- ⚠️ 𝑪𝒐𝒎𝒎𝒐𝒏 𝑪𝒂𝒖𝒔𝒆𝒔 𝒐𝒇 𝑺𝒊𝒍𝒆𝒏𝒕 𝑬𝒓𝒓𝒐𝒓𝒔: 1. Missing Strict Mode – JavaScript allows undeclared variables without "use strict". 2. Failing API Calls – A network request fails, but you never check response.ok. 3. Swallowed Errors – You used try...catch but didn’t log or handle the error. try { riskyFunction(); } catch (e) { // silence 👀 } 4. Promise Rejections – You forgot .catch() after a promise — the error goes unhandled! --- 🧠 𝑯𝒐𝒘 𝒕𝒐 𝑪𝒂𝒕𝒄𝒉 𝑺𝒊𝒍𝒆𝒏𝒕 𝑬𝒓𝒓𝒐𝒓𝒔: ✅ Use "use strict"; at the top of your code ✅ Always handle promises with .catch() ✅ Log errors inside catch blocks ✅ Use browser DevTools or linters like ESLint --- 🚀 𝐏𝐫𝐨 𝐓𝐢𝐩: Silent errors won’t crash your app — but they’ll silently break your logic. Make JavaScript talk by catching and logging everything! 🧩 #JavaScript #CodingTips #Frontend #Backend #Programmers
To view or add a comment, sign in
-
-
🚀 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁’𝘀 𝗠𝗼𝘀𝘁 𝗠𝗶𝘀𝘂𝗻𝗱𝗲𝗿𝘀𝘁𝗼𝗼𝗱 𝗞𝗲𝘆𝘄𝗼𝗿𝗱: this If you’ve ever scratched your head wondering why this doesn’t point where you expect — you’re not alone 😅 Let’s break it down 👇 What is this? • In JavaScript, this refers to 𝘁𝗵𝗲 𝗼𝗯𝗷𝗲𝗰𝘁 𝘁𝗵𝗮𝘁’𝘀 𝗲𝘅𝗲𝗰𝘂𝘁𝗶𝗻𝗴 𝘁𝗵𝗲 𝗰𝘂𝗿𝗿𝗲𝗻𝘁 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 — but its value 𝗱𝗲𝗽𝗲𝗻𝗱𝘀 𝗼𝗻 𝗵𝗼𝘄 the function is called (not where it’s written). • In Regular Function Declarations / Expressions function showThis() { console.log(this); } showThis(); // In strict mode → undefined | Non-strict → window (in browser) Here, this is determined by the 𝗰𝗮𝗹𝗹𝗲𝗿. When called directly, it defaults to the global object (window), or undefined in strict mode. • In Object Methods const user = { name: "Alice", greet() { console.log(this.name); } }; user.greet(); // "Alice" ✅ this points to the object 𝗯𝗲𝗳𝗼𝗿𝗲 𝘁𝗵𝗲 𝗱𝗼𝘁 (user in this case). In Arrow Functions const user = { name: "Alice", greet: () => console.log(this.name) }; user.greet(); // ❌ undefined Arrow functions 𝗱𝗼𝗻’𝘁 𝗵𝗮𝘃𝗲 𝘁𝗵𝗲𝗶𝗿 𝗼𝘄𝗻 this — they 𝗶𝗻𝗵𝗲𝗿𝗶𝘁 it from the surrounding (lexical) scope. So here, this refers to the global object, not user. 💡 𝗤𝘂𝗶𝗰𝗸 𝗧𝗶𝗽: Use regular functions for object methods if you need access to the object’s properties. Use arrow functions when you want to preserve the parent scope’s this (like inside a class or callback). 👉 Mastering this is one of the biggest “Aha!” moments in JavaScript. What’s your favorite trick or gotcha with this? #JavaScript #WebDevelopment #Frontend #Coding #LearnToCode #JS #this #software
To view or add a comment, sign in
-
🌱 Going back to basics In the last post, we explored how "call()", "apply()", and "bind()" can manually control what this refers to in Javascript. But what if we didn’t have to worry about "this" at all? That’s exactly where arrow functions make things simpler. The key difference between a regular function and an arrow function is this : 1. Regular functions define their own "this" (based on how they are called). 2. Arrow functions do not have their own "this", they simply inherit it from their surrounding scope. A short example : 1. regularFunction creates its own "this", so it points to user. 2. arrowFunction doesn’t! It looks outward (global scope), where "this" is undefined in "strict mode". Arrow functions simplify how we handle context by removing the need for "bind()", "call()", or "apply()", specially in modern functional code. By inheriting "this" lexically, they make context handling almost invisible!! everything just works naturally within the same scope. #JavaScript #Frontend
To view or add a comment, sign in
-
-
Async/Await — cleaner code, same engine. Let’s decode the magic behind it ⚙️👇 Ever heard the phrase — “JavaScript is asynchronous, but still runs in a single thread”? That’s where Promises and Async/Await come into play. They don’t make JavaScript multi-threaded — they just make async code smarter and cleaner 💡 Here’s a quick look 👇 // Using Promise fetchData() .then(res => process(res)) .then(final => console.log(final)) .catch(err => console.error(err)); // Using Async/Await async function loadData() { try { const res = await fetchData(); const final = await process(res); console.log(final); } catch (err) { console.error(err); } } Both do the same job — ✅ Promise handles async tasks with .then() chains ✅ Async/Await makes that flow look synchronous But what’s happening behind the scenes? 🤔 The V8 engine runs your JS code on a single main thread. When async functions like fetch() or setTimeout() are called, they’re handled by browser APIs (or libuv in Node.js). Once those tasks complete, their callbacks are queued. Then the Event Loop picks them up when the main thread is free and executes them back in the call stack. In simple words — > Async/Await doesn’t change how JavaScript works. It just gives async code a clean, readable face 🚀 That’s the power of modern JavaScript — fast, efficient, and elegant ✨ #JavaScript #AsyncProgramming #WebDevelopment #Frontend #FullStack #NodeJS #ReactJS #MERNStack #Coding #SoftwareEngineering #DeveloperLife
To view or add a comment, sign in
-
The three hardest words for any developer to say? “I don’t know.” There’s no quicker way to waste your time as a developer than to refuse to acknowledge what you don’t know. I learned this while working on my QR-Code Generator project. I had just added a dark-mode toggle button — a small, simple feature. But no matter what I did, the button refused to work. I checked my CSS, rewrote parts of the JavaScript, refreshed the browser over and over. Nothing. Hours passed before I finally paused and admitted, “I don’t know why this isn’t working.” So I searched the error message online and asked in a dev forum. Within minutes, someone pointed out the issue: my JavaScript was running before the HTML button existed — a classic DOM load-order mistake. One simple fix (adding defer to my script) solved everything. That day taught me something vital: Pride wastes time; curiosity saves it. It’s okay to say, “I don’t know.” Just don’t stop there though — ask, read, and learn from those who do. #WebDevNigeria #FrontendDev #LearningByDoing #GrowthMindset #CodingJourney
To view or add a comment, sign in
-
Explore related topics
- Clear Coding Practices for Mature Software Development
- Best Practices for Writing Clean Code
- Coding Best Practices to Reduce Developer Mistakes
- Writing Clean Code for API Development
- Simple Ways To Improve Code Quality
- Writing Elegant Code for Software Engineers
- Ways to Improve Coding Logic for Free
- Improving Code Clarity for Senior Developers
- Advanced Techniques for Writing Maintainable Code
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