June 3, 2025

Javascript Tips

JavaScript ES6 — 5 Cool Features You Should Know!

1️⃣ let & const
let x = 10;
const y = 20;

2️⃣ Arrow Function
const add = (a, b) => a + b;

3️⃣ Template Strings
let name = "Ali";
console.log(Hello, ${name}!);

4️⃣ Destructuring : 
Extract values from arrays/objects in one line
const [a, b] = [1, 2];
const {name, age} = {name: "Sara", age: 25};

5️⃣ Default Parameters : 
Set default values in functions like function greet(name = "Guest") to avoid undefined errors.
function greet(name = "Friend") {
  console.log("Hi " + name);
}

6.Quick Boolean Conversion: Use !!value to convert any value to true/false instantly.
 
7. Debounce/Throttle Events: Prevent performance issues on scroll/resize by debouncing or throttling event handlers.

8. Replace All in Strings: Use regex with /g flag: 
"potato potato".replace(/pot/g, "tom") gives "tomato tomato".

9. Memory Management: Use AbortController to clean up event listeners and avoid memory leaks.

Variables
- let x = 5; (block-scoped)
- const y = 10; (constant)
- var z = 15; (function-scoped)

Data Types
- Number: let num = 10;
- String: let str = "hello";
- Boolean: let isTrue = false;
- Array: let arr = ;
- Object: let obj = { name: "Amit", age: 25 };

Functions
- Regular:  
  function greet() { return "Hi!"; }
- Arrow:  
  const add = (a, b) => a + b;

Conditionals
- if (x > 5) { ... } else { ... }
- Ternary:  
  let result = x > 5 ? "Yes" : "No";

Loops
- For:  
  for (let i = 0; i < 5; i++) { ... }
- While:  
  while (x < 10) { ... }
- ForEach (array):  
  arr.forEach(item => console.log(item));

DOM Manipulation
- Get element:  
  document.getElementById("myId")
- Change text:  
  element.textContent = "New Text";
- Add event:  
  element.addEventListener("click", myFunc);

1. JavaScript + React = Front-end web development
2. JavaScript + Angular = Front-end web development
3. JavaScript + Vue.js = Front-end web development
4. HTML + CSS = Basic web structure and styling
5. JavaScript + Node.js = Back-end development

JavaScript + Three.js = 3D Visualization 
JavaScript + Angular = Web Applications 
JavaScript + Phaser = Game Development 
JavaScript + Vue.js = Progressive Web Apps 
JavaScript + TensorFlow.js = Machine Learning 
JavaScript + Node.js = Server-Side Development 
JavaScript + Electron = DesktopApp Development 
JavaScript + React Native = MobileApp Development
JavaScript + D3.js = Data Manipulation&Visualisation.



No comments: