- The Ternary Operator
- Alternative Assignment Operators
- Destructuring
- Spread Syntax
"Syntax sugar" means writing something in a shorter or easier way, without changing what it does. It’s just a nicer way to say the same thing.
JavaScript has some built-in syntax sugar that let us write cleaner and shorter declarations. In this chapter, we’ll look at how to assign values based on a condition, update variables with math, pull values from arrays or objects, and copy or combine them with simpler syntax.
The Ternary Operator
In JavaScript, you can assign a value based on a condition using the ternary operator, which is a short way of writing
if...else.Instead of doing:
let message if (isMorning) { message = "Good morning" } else { message = "Hello" }
You can write:
const message = isMorning ? "Good morning" : "Hello"
This means:
- If
isMorningis true, use"Good morning"Otherwise, use"Hello"
The general form is:
condition ? valueIfTrue : valueIfFalse
You can use it inside other expressions too:
console.log(isSunny ? "Go outside" : "Stay in")
Just make sure to use it for simple decisions. If the logic is complex, stick with
if...else.Alternative Assignment Operators
JavaScript has shortcut operators for doing assignments combined with operations.
Let’s look at the normal way:
let counter = 1 counter = counter + 1
This can be shortened to:
let counter = 1 counter += 1 // same as counter = counter + 1
Here are the most common ones:
| Operator | Meaning |
+= | add and assign |
-= | subtract and assign |
*= | multiply and assign |
/= | divide and assign |
Examples:
let score = 10 score += 5 // now score is 15 score *= 2 // now score is 30 score -= 10 // now score is 20
These are useful when you want to update a variable using its own value.
Destructuring
Destructuring lets you take values out of arrays or objects and store them into variables easily.
Arrays
Suppose you have:
const colors = ["red", "green", "blue"]
Instead of doing:
const first = colors[0] const second = colors[1]
You can do:
const [first, second] = colors
This assigns:
firstto"red"secondto"green"
You can skip values too:
const [,, third] = colors console.log(third) // blue
Objects
You can extract values from objects too:
const user = { name: "Alice", age: 30 } const { name, age } = user console.log(name) // Alice console.log(age) // 30
If the property has a different name than the variable you want, you can rename it:
const { name: username } = user console.log(username) // Alice
Destructuring makes your code cleaner when working with objects and arrays.
Spread Syntax
The spread syntax uses
... to unpack or copy values.Arrays
You can copy or merge arrays:
const a = [1, 2] const b = [3, 4] const both = [...a, ...b] console.log(both) // [1, 2, 3, 4]
You can also clone an array:
const original = [10, 20, 30] const clone = [...original]
Objects
You can do the same with objects:
const a = { x: 1 } const b = { y: 2 } const merged = { ...a, ...b } console.log(merged) // { x: 1, y: 2 }
You can also override values:
const user = { name: "Alice", age: 30 } const updated = { ...user, age: 31 } console.log(updated) // { name: "Alice", age: 31 }
This is very useful when updating objects without changing the original.