← Back to Examples

Variables & Data Types

Open the browser console (F12) to see additional output!

1. Variable Declaration

let name = 'John'; // Can be reassigned
const PI = 3.14159; // Cannot be reassigned
var oldWay = 'avoid'; // Old way, don't use
Click "Run Demo" to see output...

2. Data Types

// Primitive types
let str = 'Hello'; // String
let num = 42; // Number
let bool = true; // Boolean
let nothing = null; // Null
let notDefined; // Undefined
Click "Run Demo" to see output...

3. Template Literals

let firstName = 'John';
let lastName = 'Doe';
let age = 25;

// Old way (concatenation)
'Hello, ' + firstName + ' ' + lastName

// New way (template literals)
`Hello, ${firstName} ${lastName}! You are ${age} years old.`
Click "Run Demo" to see output...

4. Type Checking

typeof 'hello' // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" (JS quirk!)
typeof [] // "object"
typeof {} // "object"
Click "Run Demo" to see output...

5. Arrays

let fruits = ['apple', 'banana', 'orange'];
let numbers = [1, 2, 3, 4, 5];

fruits[0] // 'apple' (first element)
fruits.length // 3
fruits.push('mango') // Add to end
Click "Run Demo" to see output...

6. Objects

let person = {
name: 'John',
age: 25,
isStudent: true,
greet: function() {
return `Hello, I'm ${this.name}`;
}
};
Click "Run Demo" to see output...

← Back to Examples