
JavaScript Day 2: Mastering Assignment, Unary, Boolean Operators, and More
In our previous blog, we explored the foundational concepts of JavaScript data types and variables, helping you understand how data is declared, stored, and recognized in your code. That knowledge serves as the groundwork for what we will learn today.
In this blog, we will be exploring more hands-on concepts that directly affect how your JavaScript code executes. We will cover:
- Assignment Operators
- Unary Operators
- Boolean Logic
- String Handling and String Indices
- null vs undefined
Assignment Operators in JavaScript
Assignment operators allow us to assign values to variables. While = is the most basic assignment operator, JavaScript offers compound operators that both perform an operation and assign the result to the variable.
Examples:
let x = 5; x += 3; x *= 2; x %= 4;
Conceptual Question:
What happens if you write this?
let a = 7; a **= 2;
Answer:
The value of a becomes 49 because **= is the exponentiation assignment operator, which raises a to the power of 2.
Practice:
let b = 10; b -= 3; console.log(b); // Output is 7 because -= subtracts 3 from 10.
Unary Operators in JavaScript
Unary operators act on a single operand. These include:
- ++ increment
- -- decrement
- ! logical NOT
- unary plus
- unary negation
Examples:
let y = 5; y++; let z = !true; let a = +"10";
Conceptual Question:
let y = 4; console.log(++y);
Answer:
Output is 5 because ++y is a prefix increment — value increases before logging.
Practice:
let x = "20"; console.log(-x); // Output is -20. The string "20" is converted to number, then negated.
Boolean Operators in JavaScript
Boolean operators help in decision making and logical flow control. The primary ones are:
- && AND
- || OR
- ! NOT
They return either a boolean or the last evaluated operand.
Examples:
let isTrue = true; let isFalse = false; console.log(isTrue && isFalse); console.log(isTrue || isFalse); console.log(!isTrue);
Conceptual Question:
console.log(0 || "hello");
Answer:
Output is "hello" because || returns the first truthy value.
Practice:
let a = null; let b = "world"; console.log(a ?? b); // Output is "world" because the nullish coalescing operator ?? returns the right-hand side if the left is null or undefined.
Strings in JavaScript
Strings are sequences of characters and can be declared with single quotes, double quotes, or backticks.
Examples:
let str = "Hello"; console.log(str.length); console.log(str.toUpperCase()); console.log(str.charAt(0));
Conceptual Question:
console.log("hello".split(""));
Answer:
["h", "e", "l", "l", "o"] — because split("") divides the string into characters.
Practice:
let s = "JavaScript"; console.log(s.indexOf("Script")); // output is 4 — because "Script" begins at index 4.
String Indices in JavaScript
String indices are 0-based, meaning they start at 0. Strings are immutable — you cannot change a character, only access it.
Examples:
let str = "world"; console.log(str[0]); console.log(str.charAt(4));
Conceptual Question:
console.log("test"[12]);
Answer:
undefined — because index 12 does not exist in a 4-character string.
Practice:
let s = "example"; console.log(s[2]);
Explanation:
"a" — character at index 2 is "a".
Null vs Undefined in JavaScript
Both null and undefined mean “no value”, but they differ:
- undefined: variable is declared but not assigned
- null: a deliberate assignment of no value
Examples:
let a; console.log(a); let b = null; console.log(b);
Conceptual Question:
console.log(undefined == null); console.log(undefined === null);
Answer:
true and false. The first is loose equality (type conversion allowed), the second is strict (no type conversion).
Practice:
let x; let y = null; console.log(typeof x); console.log(typeof y); // "undefined" and "object" — typeof null returns "object" due to a historical JavaScript behavior.
Practice Roundup with Detailed Explanations
Assignment Operators:
let x = 5; x += 2; x *= 3; console.log(x);
Explanation:
x starts at 5, becomes 7 after += 2, then becomes 21 after *= 3.
Unary Operators:
let a = 10; let b = a++; let c = ++a; console.log(b, c); // b is 10 (post-increment), c is 12 (pre-increment).
Boolean Operators:
console.log(0 && "hello"); console.log("" || "world"); // First is 0 (falsy), second is "world" (first truthy).
Strings:
let s = "Hello, world!"; console.log(s.replace("world", "JavaScript")); // "Hello, JavaScript!" — replace() replaces the first match.
String Indices:
let str = "example"; console.log(str[str.length - 1]); // Last character is "e".
Null and Undefined:
let a; let b = null; console.log(a === undefined); console.log(b === null); // Both return true — a is undefined, b is null.
Final Words
Understanding JavaScript’s operators and special values is crucial for writing clean, efficient, and bug-free code. Whether it is knowing how null behaves differently from undefined, or how a string method like charAt works, these basics are the foundation of every serious JavaScript developer’s skill set.
In the next blog, we will explore conditional statements, loops, and flow control, helping you take the logic and decision-making in JavaScript to the next level.
Practice what you have learned, and try editing the examples to deepen your understanding.
0 Comments