Variables (let): Re-declarable, re-assignable.
let
Constants (const): Cannot be re-declared or re-assigned.
const
Comments:
Single-line: // This is a comment
// This is a comment
Multi-line: /* This is a multi-line comment */
/* This is a multi-line comment */
let age = 25; // Variable (mutable)
const PI = 3.14159; // Constant (immutable)
// This function calculates area
function calculateArea() {
/* Multi-line comment
explaining the function
*/
}
camelCase: userName (variables, functions).
userName
PascalCase: UserProfile (classes, constructors).
UserProfile
snake_case: user_name (rare in JS, used in URLs).
user_name
UPPER_SNAKE_CASE: MAX_USERS (constants).
MAX_USERS
let userName = "Alice"; // camelCase
class UserProfile {} // PascalCase
const MAX_USERS = 100; // UPPER_SNAKE_CASE
JavaScript has primitive and reference types:
Type
Example
Description
string
"Hello"
Text
number
42, 3.14
42
3.14
Integers/floats
boolean
true, false
true
false
Logical values
null
Intentional "no value"
undefined
Variable declared but not assigned
object
{ name: "Bob" }
Includes arrays, functions
symbol
Symbol()
Unique identifier
let name = "Bob"; // string
let score = 10; // number
let isLoggedIn = false; // boolean
let user = null; // null
let data; // undefined
let person = { age: 30 }; // object
Strings are immutable (cannot be changed after creation). Use methods to manipulate them.
length: Gets string length.
length
toUpperCase()/toLowerCase(): Changes case.
toUpperCase()
toLowerCase()
slice(start, end): Extracts part of a string.
slice(start, end)
replace(old, new): Replaces substring.
replace(old, new)
includes(search): Checks if substring exists.
includes(search)
let message = "Hello, World!";
console.log(message.length); // 13
console.log(message.toUpperCase()); // "HELLO, WORLD!"
console.log(message.slice(7, 12)); // "World"
Integers, floats, and NaN (Not-a-Number).
Use Math object for operations (e.g., Math.random()).
Math
Math.random()
let sum = 10 + 5; // 15
let price = 9.99; // float
let random = Math.random(); // 0 β€ random < 1
Use backticks (`) for multi-line strings and embed expressions.
let name = "Alice";
let age = 25;
console.log(`Hello, ${name}! You are ${age} years old.`);
// Output: "Hello, Alice! You are 25 years old."
Ordered lists of values. Use methods to add/remove items.
push(): Adds to end.
push()
pop(): Removes from end.
pop()
slice(): Extracts part of array.
slice()
let fruits = ["apple", "banana", "cherry"];
fruits.push("date"); // ["apple", "banana", "cherry", "date"]
let last = fruits.pop(); // "date" (array becomes ["apple", "banana", "cherry"])
null: Assigned to indicate "no value".
undefined: Value not assigned or function returns no value.
let data = null; // Intentional absence of value
let result; // undefined (not initialized)
function noReturn() {}
console.log(noReturn()); // undefined
Comparisons:
== (loose), === (strict).
==
===
>, <, >=, <=, ! (not).
>
<
>=
<=
!
let a = 5;
let b = "5";
console.log(a == b); // true (loose comparison)
console.log(a === b); // false (strict comparison: different types)
Loose (==): Converts types before comparing.
Strict (===): No type conversion.
console.log(0 == false); // true (0 == 0 β true)
console.log(0 === false); // false (different types)
Implicit: JS converts types automatically (e.g., "5" + 2 = "52").
"5" + 2 = "52"
Explicit: Use Number(), String(), Boolean().
Number()
String()
Boolean()
console.log("5" + 2); // "52" (implicit string concatenation)
console.log(Number("5") + 2); // 7 (explicit conversion)
A: let allows reassignment; const does not.
A: null is an intentional absence of value; undefined means uninitialized.
A: Always use === to avoid unexpected type coercion.
NaN
A: Not-a-Number. It results from invalid numeric operations (e.g., 0/0).
0/0
A: They allow embedded expressions (\${}) and multi-line strings without concatenation.
\${}
Create a program that:
Declares a const for PI (3.14159).
PI
Defines an array of your favorite foods.
Uses a template literal to log: "I love \${food1}, \${food2}, and \${food3}!".
"I love \${food1}, \${food2}, and \${food3}!"
Checks if 100 == "100" and 100 === "100", logging both results.
100 == "100"
100 === "100"
Converts the string "42" to a number and add it to PI.
"42"
const PI = 3.14159;
let favoriteFoods = ["pizza", "sushi", "tacos"];
console.log(`I love ${favoriteFoods[0]}, ${favoriteFoods[1]}, and ${favoriteFoods[2]}!`);
console.log(100 == "100"); // true (loose comparison)
console.log(100 === "100"); // false (strict comparison)
let num = Number("42");
console.log(PI + num); // 45.14159
Last changed20 days ago