JavaScript
Syntax and Complex Types
JavaScript is flexible, powerful, and sometimes quirky. Below is a comprehensive guide covering syntax basics, complex types, and advanced structures you need to understand to write maintainable code.
Basic Syntax
Variables
let name = "Alice"; // Mutable variable
const PI = 3.14; // Immutable constant
var legacy = 42; // Avoid using varFunctions
function greet(name) {
return `Hello, ${name}`;
}
const add = (a, b) => a + b;Conditionals & Loops
if (score > 90) {
console.log("Excellent");
} else {
console.log("Keep trying");
}
for (let i = 0; i < 5; i++) {
console.log(i);
}Complex Types
Arrays
const colors = ["red", "green", "blue"];
console.log(colors[1]); // "green"Can hold mixed types (not recommended):
const mixed = [1, "hello", true, { key: "value" }];Objects
const person = {
name: "Alice",
age: 30,
greet: function() {
return `Hi, I'm ${this.name}`;
}
};
console.log(person.name); // "Alice"
console.log(person.greet()); // "Hi, I'm Alice"Nested Objects (Objects of Objects)
const company = {
name: "TechCorp",
address: {
street: "123 Main St",
city: "Tech City",
location: {
lat: 40.7128,
lng: -74.0060
}
},
employees: {
manager: { name: "Bob", age: 45 },
developer: { name: "Carol", age: 28 }
}
};
console.log(company.address.city); // "Tech City"
console.log(company.address.location.lat); // 40.7128
console.log(company.employees.manager.name); // "Bob"Maps (Key-Value Store)
const map = new Map();
map.set('id', 1);
map.set('name', 'Alice');
console.log(map.get('name')); // "Alice"
console.log(map.has('id')); // trueSets (Unique Collection of Values)
const set = new Set();
set.add(1);
set.add(2);
set.add(1); // Duplicate ignored
console.log(set.has(1)); // true
console.log(set.size); // 2Symbols (Unique Identifiers)
const sym1 = Symbol('id');
const sym2 = Symbol('id');
console.log(sym1 === sym2); // false
const obj = {
[sym1]: 'unique value'
};
console.log(obj[sym1]); // "unique value"Special Types
Null vs Undefined
let a;
console.log(a); // undefined
let b = null;
console.log(b); // nullType Checking
typeof 123 // "number"
typeof "hello" // "string"
typeof true // "boolean"
typeof {} // "object"
typeof [] // "object" (Arrays are objects)
typeof null // "object" // quirk in JSComplex Type Example
const user = {
id: 1,
name: "Alice",
roles: ["admin", "editor"],
profile: {
email: "alice@example.com",
address: {
street: "1 Wonderland Drive",
city: "Wonderland",
zip: "12345"
}
},
isActive: true
};
console.log(user.profile.address.city); // "Wonderland"
console.log(user.roles[0]); // "admin"Practical Tips
- Avoid
var; preferletandconst. - Use Maps when keys are not strings or need guaranteed key order.
- Use Sets to store unique items efficiently.
- Symbols are great for adding private-ish properties.
- Deep cloning nested objects requires libraries like
lodashor structured cloning.
Properties
JavaScript is a powerful and flexible language, but what exactly makes it tick? Here are the core properties that define how it works and why it’s so popular.
Async JS
JavaScript isn’t just about synchronous code — mastering asynchronous patterns is essential. Below is a complete guide covering Callbacks, Functional Arguments, Callback Hell, and the Event Loop.