Grammar & Types
Basics
JavaScript is case-sensitive and uses Unicode. Instructions are called statements and are separated by semicolons (;). While JavaScript has Automatic Semicolon Insertion (ASI), it's best practice to always include explicit semicolons.
Comments
Two types of comments are supported:
// Single-line comment
/* Multi-line comment
can span multiple lines */
#!/usr/bin/env node // Hashbang comment (valid in scripts)
Variable Declarations
Three ways to declare variables exist in modern JavaScript:
var x = 42; // Function-scoped (avoid in modern code)
let y = 13; // Block-scoped, reassignable
const PI = 3.14; // Block-scoped, read-only
Variable Scope
- Global scope: Variables accessible everywhere
- Module scope: Variables accessible within a module
- Function scope: Variables accessible within a function (var only)
- Block scope: Variables accessible within
{}(let/const only)
Hoisting
vardeclarations are hoisted to the top of their function scopeletandconstare in the "temporal dead zone" — they're hoisted but not initialized, causing ReferenceError if accessed before declaration
console.log(x); // undefined (hoisted)
var x = 5;
console.log(y); // ReferenceError (temporal dead zone)
let y = 5;
Data Types
JavaScript has eight ECMAScript data types: seven primitives and one complex type.
Primitive Types
- Boolean:
trueorfalse - null: Explicitly represents no value
- undefined: Represents uninitialized or missing values
- Number:
42or3.14159(IEEE 754 double-precision) - BigInt:
9007199254740992n(arbitrary-precision integers) - String:
"Hello"or'World' - Symbol: Unique and immutable identifiers
Complex Type
- Object: Named containers for values and methods
Dynamic Typing
JavaScript is dynamically typed — variables can change types at runtime:
let answer = 42;
answer = "Thanks for all the fish!"; // Valid - type changed
Type Conversion
Automatic Coercion
The + operator with operands triggers string conversion:
x = "The answer is " + 42; // "The answer is 42"
y = "37" + 7; // "377"
Other operators convert to numbers:
"37" - 7; // 30
"37" * 7; // 259
"37" / 7; // 5.285...
Explicit Conversion
Convert strings to numbers:
parseInt("101", 2); // 5 (binary with radix)
parseFloat("3.14"); // 3.14
Number("42"); // 42
+"1.1" + +"1.1"; // 2.2 (unary plus operator)
Literals
Array Literals
const coffees = ["French Roast", "Colombian", "Kona"];
const fish = ["Lion", , "Angel"]; // Note: empty slot (undefined)
Boolean Literals
true
false
Numeric Literals
// Decimal (base 10)
0, 117, 123456789123456789n
// Octal (base 8)
015, 0o777777777777n
// Hexadecimal (base 16)
0x1123, 0x123456789ABCDEFn
// Binary (base 2)
0b11, 0b11101001010101010101n
// Floating-point
3.1415926, .123456789, 3.1E+12
Object Literals
const car = { myCar: "Saturn", getCar: carTypes("Honda") };
const nested = { manyCars: { a: "Saab", b: "Jeep" }, 7: "Mazda" };
// ES6 Enhanced object literals
const obj = {
__proto__: theProtoObj,
handler, // Shorthand for handler: handler
toString() { return `d ${super.toString()}`; },
["prop_" + (() => 42)()]: 42 // Computed property names
};
RegExp Literals
const re = /ab+c/;
const pattern = /[a-z]+/gi; // With flags
String Literals
'Single quotes'
"Double quotes"
'Escape: \n new line'
// Template literals (backticks) - multiline and interpolation
const name = "Lev";
`Hello ${name}, how are you?`
`In JavaScript, template strings can run
over multiple lines`
// Tagged templates
print`I need to do:
${todos}
My current progress is: ${progress}`;
Special Characters in Strings
\0 // Null Byte
\b // Backspace
\f // Form Feed
\n // New Line
\r // Carriage Return
\t // Tab
\' // Apostrophe
\" // Double quote
\\ // Backslash
\xA9 // Latin-1 encoding (© symbol)
© // Unicode escape
\u{2F804} // Unicode code point (ES6)
Constants
Use const to declare constants. Note that const prevents reassignment, not mutation:
const PI = 3.14;
// PI = 3.15; // Error: Assignment to constant
const MY_OBJECT = { key: "value" };
MY_OBJECT.key = "otherValue"; // Allowed - mutating the object
const MY_ARRAY = ["HTML", "CSS"];
MY_ARRAY.push("JAVASCRIPT"); // Allowed - mutating the array
Best Practices
- Always include semicolons explicitly
- Use
constby default,letwhen reassignment is needed, avoidvar - Always provide a radix parameter with
parseInt()for clarity - Use template literals for string interpolation
- Be aware of hoisting behavior when using
var - Declare all variables near the top of their scope for clarity