JavaScript Basics: Values, Types, and Operators
π’ Numbers
Numbers represent all kinds of numeric data in JavaScript.
let age = 25;
let pi = 3.14;
let speedOfLight = 2.998e8; // 2.998 Γ 10^8
JavaScript uses 64 bits to store each number β enough to represent most values, but not all decimals are exact.
Arithmetic Operators
console.log(100 + 4 * 11); // 144
console.log((100 + 4) * 11); // 1144
console.log(10 % 3); // 1 (remainder)
Common operators:
+addition-subtraction*multiplication/division%remainder (modulo)
Special Numbers
console.log(Infinity - 1); // Infinity
console.log(0 / 0); // NaN (Not a Number)
π§΅ Strings
Strings represent text and are wrapped in quotes.
let message = "Hello, world!";
let lineBreak = "First line\nSecond line";
You can combine strings using +:
console.log("con" + "cat" + "enate"); // concatenate
Template literals (backticks) allow embedding values:
let x = 100;
console.log(`Half of ${x} is ${x / 2}`);
// β Half of 100 is 50
βοΈ Booleans
Booleans are simple true/false values used for logic and conditions.
console.log(3 > 2); // true
console.log(3 < 2); // false
Comparison Operators
| Operator | Meaning |
|---|---|
> | greater than |
< | less than |
>= | greater or equal |
<= | less or equal |
== | equal (with type conversion) |
=== | equal (strict, no conversion) |
!= | not equal |
!== | not equal (strict) |
Example:
console.log("Garnet" != "Ruby"); // true
console.log(NaN == NaN); // false
π£ Logical Operators
Logical operators let you combine Boolean values:
console.log(true && false); // false
console.log(true || false); // true
console.log(!true); // false
&&β AND (both must be true)||β OR (at least one true)!β NOT (flips the value)
Short-Circuiting
console.log(null || "user"); // "user"
console.log("Agnes" || "user"); // "Agnes"
||returns the first truthy value.&&returns the first falsy value.??(nullish coalescing) only checks fornullorundefined.
console.log(0 || 100); // 100
console.log(0 ?? 100); // 0
console.log(null ?? 100); // 100
π§© Other Useful Operators
typeof
Tells you the type of a value.
console.log(typeof 4.5); // "number"
console.log(typeof "hi"); // "string"
Ternary Operator (?:)
A compact way to write conditions.
let age = 20;
console.log(age > 18 ? "Adult" : "Minor"); // "Adult"
π« Empty Values
null and undefined represent the absence of a value.
let x;
console.log(x); // undefined
x = null;
console.log(x); // null
π Type Conversion
JavaScript often converts types automatically β sometimes in confusing ways!
console.log(8 * null); // 0
console.log("5" - 1); // 4
console.log("5" + 1); // "51"
console.log("five" * 2); // NaN
console.log(false == 0); // true
Use === to avoid unwanted conversions.
π§ Summary
- Numbers, Strings, Booleans, null, and undefined are the basic value types.
- Operators (
+,-,*,/,%,==,&&,||, etc.) let you combine and transform values. - Use
===and!==to avoid confusing type conversions. - Strings use backticks (
`) for embedded expressions. - Logical operators like
||and??can set default values.
πͺ With these basics, you can already use JavaScript like a calculator that speaks logic β and youβre ready for the next step: building real programs!