💻 JavaScript Intermediate – Check if Two Strings are Anagrams An anagram is when two words contain the same letters in a different order. 📌 Problem: Check if two strings are anagrams of each other. function isAnagram(str1, str2) { let a = str1.split("").sort().join(""); let b = str2.split("").sort().join(""); return a === b; } console.log(isAnagram("listen", "silent")); 📤 Output: true 📖 Explanation: • Use split("") to convert strings into arrays. • sort() arranges letters in alphabetical order. • join("") converts arrays back to strings. • If the sorted strings are equal, they are anagrams. 💡 Tip: This method works for case-sensitive anagrams; you can use .toLowerCase() for case-insensitive checks. #JavaScript #Coding #WebDevelopment #FrontendDevelopment #LearnToCode #ProgrammingTips
Check if Two Strings are Anagrams in JavaScript
More Relevant Posts
-
🔍 JavaScript Logic That Looks Simple… But Isn’t! Ever wondered how this works? 👇 if (!isNaN(str[i]) && str[i] % 2 == 0) { console.log(str[i]); } Let’s break it down: Suppose: let str = "a1b2c3d4"; 🔹 Step 1: str[i] Gives each character → "a", "1", "b", "2"... 🔹 Step 2: !isNaN(str[i]) Checks if the character can be converted to a number "2" → valid number ✅ "a" → not a number ❌ 🔹 Step 3: str[i] % 2 == 0 JavaScript converts "2" → 2 Then checks if it’s even ⚡ Hidden Magic: Short-Circuit (&&) If the first condition fails, JavaScript skips the second condition So for "a": !isNaN("a") → false % 2 is never executed 🚫 ✅ Final Output: 2 4 🚀 Key Takeaways: ✔ JavaScript automatically converts strings to numbers in calculations ✔ isNaN() helps filter numeric values ✔ && prevents errors using short-circuiting 💬 Clean logic like this is often used in string parsing & interview problems Would you like more such real-world JS tricks? 👇 #JavaScript #WebDevelopment #Coding #100DaysOfCode #LearnToCode
To view or add a comment, sign in
-
Most JavaScript developers get confused with this 🤯 But once you understand how a function is called, everything becomes clear. ⚡ What is this in JavaScript? this is a keyword that refers to the object that is executing the current function. But the tricky part is… 👉 this changes depending on how the function is called. Example: const user = { name: "John", sayHi: function() { console.log("Hi, " + this.name); } }; user.sayHi(); // Hi, John Here this refers to the user object. 💡 Key things to remember ✔ this depends on how the function is called ✔ Inside an object method → this refers to that object ✔ Arrow functions → use the parent scope this ✔ call() / apply() can manually change this 📌 Pro Tip: Always check how the function is called, not where it is written. If you're learning JavaScript, mastering this will save you from many debugging headaches later. Follow for more JavaScript concepts explained simply. 🚀 #javascript #webdevelopment #frontenddevelopment #coding #programming #js #learnjavascript #softwaredeveloper #100daysofcode #developer
To view or add a comment, sign in
-
-
💻 JavaScript Intermediate – Custom map() Function The map() method is widely used to transform arrays. Here’s how you can implement it manually. 📌 Problem: Apply a function to each element of an array and return a new array. function customMap(arr, callback) { let result = []; for (let i = 0; i < arr.length; i++) { result.push(callback(arr[i])); } return result; } let numbers = [1, 2, 3]; let doubled = customMap(numbers, function(num) { return num * 2; }); console.log(doubled); 📤 Output: [2, 4, 6] 📖 Explanation: • map() creates a new array by applying a function to each element. • Here, we manually implemented the same logic using a loop and callback. 💡 Tip: Understanding this helps you grasp how higher-order functions work in JavaScript. #JavaScript #Coding #WebDevelopment #FrontendDevelopment #LearnToCode #ProgrammingTips
To view or add a comment, sign in
-
-
💡 Understanding Scope in JavaScript One of the most important concepts in JavaScript is Scope. Scope defines where variables can be accessed in your code. There are three main types of scope in JavaScript: 🔹 Global Scope Variables declared outside any function are accessible anywhere in the program. let name = "Muneeb"; function showName() { console.log(name); } showName(); // Accessible because it's global 🔹 Function Scope Variables declared inside a function can only be used inside that function. function greet() { let message = "Hello"; console.log(message); } greet(); // console.log(message); ❌ Error 🔹 Block Scope Variables declared with "let" and "const" inside "{ }" are only accessible within that block. if (true) { let age = 25; console.log(age); } // console.log(age); ❌ Error 📌 Understanding scope helps developers write cleaner code and avoid bugs related to variable access. Mastering these fundamentals makes JavaScript much easier to understand and improves problem-solving skills. #JavaScript #WebDevelopment #FrontendDeveloper #Coding #LearnToCode
To view or add a comment, sign in
-
I used to write JavaScript strings like this… and it was painful. "Hello " + name + " welcome to JavaScript" Too many + signs. Hard to read. Easy to break. Then I discovered Template Literals in JavaScript. Suddenly strings became cleaner, more powerful, and much easier to manage. You can now: • Embed variables → ${name} • Embed expressions → ${total + tax} • Write multi-line strings easily • Improve code readability Example: let text = `Hello, ${name}! Welcome to JavaScript.`; Small feature. Huge improvement for writing clean JavaScript. I just published a quick blog explaining Template Literals with examples 👇 https://lnkd.in/dytp3HtH Would love your feedback and thoughts! Do you prefer template literals or string concatenation in JavaScript? 🤔 Hitesh Choudhary | Piyush Garg | Akash Kadlag | Shubham Waje | Suraj Kumar Jha #chaicode #JavaScript #WebDevelopment #Programming #LearnToCode #100DaysOfCode ``` 🚀
To view or add a comment, sign in
-
-
"If you’re not using "map", "filter", and "reduce" in JavaScript… you're probably writing more code than needed." 😅 These 3 array methods can level up your code instantly 👇 🔹 map() 👉 Transforms each element of an array 👉 Returns a new array 💻 Example: const nums = [1, 2, 3]; const doubled = nums.map(n => n * 2); // [2, 4, 6] 🔹 filter() 👉 Filters elements based on a condition 👉 Returns a new array 💻 Example: const nums = [1, 2, 3, 4]; const even = nums.filter(n => n % 2 === 0); // [2, 4] 🔹 reduce() 👉 Reduces array to a single value 👉 Very powerful (but often misunderstood) 💻 Example: const nums = [1, 2, 3, 4]; const sum = nums.reduce((acc, curr) => acc + curr, 0); // 10 🚀 Pro Tip: Use "map" for transformation, "filter" for selection, and "reduce" for everything else. 💬 Which one do you use the most in your projects? #javascript #webdevelopment #mern #coding #developers
To view or add a comment, sign in
-
🔥 Regular Function vs Arrow Function in JavaScript — what's the real difference? This confused me when I started. Here's everything you need to know 👇 ━━━━━━━━━━━━━━━━━━━━━━━━━━ 1. The syntax is different Regular functions use the function keyword. Arrow functions use a shorter ( ) => syntax — less typing, cleaner code. 2. The "this" keyword behaves differently This is the BIG one. 📌 Regular function → has its own this. It changes depending on HOW the function is called. 📌 Arrow function → does NOT have its own this. It borrows this from the surrounding code. That's why arrow functions are great inside classes and callbacks — they don't lose track of this. 3. Arrow functions can't be used as constructors You can do new regularFunction() — works fine. You can NOT do new arrowFunction() — it throws an error. 4. Arguments object Regular functions have a built-in arguments object to access all passed values. Arrow functions don't — use rest parameters (...args) instead. ━━━━━━━━━━━━━━━━━━━━━━━━━━ ✅ When to use which? → Use arrow functions for short callbacks, array methods (.map, .filter), and when you need to keep this from the outer scope. → Use regular functions when you need your own this, use as a constructor, or need the arguments object. Understanding this = leveling up as a JavaScript developer 🚀 #JavaScript #WebDevelopment #100DaysOfCode #Frontend #CodingTips #Programming
To view or add a comment, sign in
-
🚀 JavaScript Object Cheat Sheet Every Developer Should Know Objects are the heart of JavaScript. Almost everything in JavaScript revolves around objects, properties, and methods. If you master objects, you automatically level up your JavaScript skills. Here’s a quick JavaScript Object Cheatsheet to remember the most useful patterns: 🔹 Object Declaration const user = { name: "Profile", followers: 4817 } 🔹 Access Properties user.name // dot notation user["name"] // bracket notation 🔹 Delete Property delete user.name 🔹 Iterate Objects for (const key in user) { console.log(key, user[key]) } 🔹 Copy Object const copy = {...user} // shallow copy const deepCopy = structuredClone(user) // deep copy 🔹 Freeze Object Object.freeze(user) 🔹 Destructuring const { name, followers } = user 🔹 Getter & Setter const user = { name: "Profile", get profile(){ return this.name } } 💡 Pro Tip: Using destructuring, spread operator, and Object.freeze() properly makes your JavaScript code cleaner and safer. 📌 Save this cheat sheet so you can quickly review JavaScript objects anytime. If this helped you: 👍 Like 💬 Comment your favorite JavaScript feature 🔁 Share with a developer friend Follow for more JavaScript Cheat Sheets & Developer Tips 🔗 LinkedIn: mdyousufali205 #javascript #webdevelopment #programming #coding #frontend #softwareengineering #developers #javascriptdeveloper #codingtips #devcommunity #learnprogramming #100DaysOfCode #webdev
To view or add a comment, sign in
-
-
🔍 Regular Functions vs Arrow Functions in JavaScript A quick comparison every developer should know: 👉 Syntax Regular: function add(a, b) { return a + b } Arrow: const add = (a, b) => a + b 👉 this Behavior Regular functions have their own this (depends on how they are called) Arrow functions inherit this from their surrounding scope 👉 Usage as Methods Regular functions work well as object methods Arrow functions are not suitable as methods due to lexical this 👉 Constructors Regular functions can be used with new Arrow functions cannot be used as constructors 👉 Arguments Object Regular functions have access to arguments Arrow functions do not have their own arguments 👉 Return Behavior Regular functions require explicit return Arrow functions support implicit return for single expressions 👉 Hoisting Regular function declarations are hoisted Arrow functions behave like variables and are not hoisted the same way 💡 When to Use What? ✔ Use regular functions for methods, constructors, and dynamic contexts ✔ Use arrow functions for callbacks, cleaner syntax, and functional patterns Choosing the right one can make your code more predictable and easier to maintain. #JavaScript #WebDevelopment #FrontendDevelopment #CodingTips #Developers
To view or add a comment, sign in
-
JavaScript fun facts that sound fake but are actually real: - "typeof null" → ""object"" (this is a bug from early JS that was never fixed) --- - "[] + []" → """" (empty string) --- - "[] + {}" → ""[object Object]"" --- - "{} + []" → "0" (yes… seriously) --- - "NaN === NaN" → "false" (the only value not equal to itself) --- - "0.1 + 0.2 !== 0.3" (floating point precision issue) --- - Functions are objects in JavaScript → you can add properties to them --- - JavaScript is single-threaded → but still handles async like a pro using event loop --- - "setTimeout(fn, 0)" does NOT run immediately → it runs after the call stack is empty --- If JavaScript ever feels weird, it’s not you. It’s JavaScript. Still learning, still questioning. #JavaScript #WebDevelopment #FrontendDevelopment #Programming #Developers #CodingJourney #TechFacts #BuildInPublic
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