Variables allow you to store and manage data in JavaScript.
Understanding variables and data types is essential before writing any meaningful application.
Declaring Variables
JavaScript provides three ways to declare variables:
var name = "Emre";
let age = 30;
const country = "Turkey";var
- Old JavaScript syntax
- Function-scoped
- Avoid in modern code
let
- Block-scoped
- Use when value will change
const
- Block-scoped
- Value cannot be reassigned
Primitive Data Types
JavaScript has seven primitive types.
Primitive means the value is not an object and has no methods attached by default.
1. String
let city = "Antalya";Used for text data.
2. Number
let score = 95;
let price = 12.99;JS uses a single numeric type for integers and decimals.
3. Boolean
let isLoggedIn = true;
let isAdmin = false;Represents true or false values.
4. Null
Represents "intentional absence" of a value.
let car = null;5. Undefined
Means a variable was declared but no value assigned.
let address;
console.log(address); // undefined6. Symbol
Used for unique identifiers (advanced topic).
7. BigInt
let big = 12345678901234567890n;For very large numbers beyond Number limits.
Checking Data Types with typeof
The typeof operator returns the type of a value.
console.log(typeof "Hello"); // string
console.log(typeof 42); // number
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof null); // object (JavaScript bug)Note: typeof null returning "object" is a long-known JS quirk.
Dynamic Typing
JavaScript is dynamically typed.
This means a variable’s type can change at runtime.
let value = 100;
value = "now a string";Useful but dangerous if not used carefully.
Summary
In this lesson, you learned:
- How to declare variables using var, let, const
- What primitive data types exist
- How typeof works
- Why JavaScript is dynamically typed
In the next lesson, we will explore operators and expressions.