JavaScript is fantastic, no argue. In this post we’re diving into some fantastic syntaxes and features.

Immediately Invoked Function Expression (IIFE)

They are functions that run as soon as it’s defined. Ususally seen in bundled js files. It wont export any variable or method to the global scope.

// Standard IIFE
(function () {
  console.log("Hello from IIFE!");
})();

// Arrow function IIFE
(() => {
  console.log("Arrow IIFE here!");
})();

// Async IIFE
(async () => {
  console.log("Async IIFE in action!");
})();

Learn more about IIFE


The Comma Operator (,)

It lets you evaluate multiple expressions and returns the last one. Here’s how it works:

let x = 1;

x = (x++, x); // Increment x, then assign the new value of x
console.log(x); // Output: 2

x = (2, 3); // Only the last value matters
console.log(x); // Output: 3

More on the comma operator


Generator Functions (function*)

Generators are like functions with a pause button. They let you yield values one at a time:

function* generator(i) {
  yield i;
  yield i + 10;
}

const gen = generator(10);

console.log(gen.next().value); // Output: 10
console.log(gen.next().value); // Output: 20

Deep dive into generators


Functions: A Quick Comparison

⚠ Created by ChatGPT 🤖

Here’s a handy table comparing function declarations and expressions:

FeatureFunction DeclarationFunction Expression
HoistingYes (hoisted to the top of the scope)No (only accessible after assignment)
Syntaxfunction name() { ... }const name = function() { ... }
UsageCan be called before it’s definedMust be defined before calling
Anonymous?No, always namedYes, can be anonymous
Arrow Functions?NoYes, supports arrow functions

Quick CSS Styling with JavaScript

Neat trick to apply CSS:

const style = {
  width: "50px",
  height: "50px",
  borderRadius: "25px", // CamelCase for CSS properties
};

Object.assign(container.style, style);

That’s it for now! JavaScript is full of these little gems, so stay curious and keep exploring!