ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)
Set your Publisher ID in src/config/ads.ts to display live ads.
JavaScript Tutorial• 5 min read
JavaScript Variables & Data Types
Declaring data with let, const, and understanding primitive types
Variables are containers for storing data (storing data values). JavaScript variables can be declared using let, const, or var.
let vs const vs var
const: Used for variables whose values should never change (block-scoped). Always use const by default unless you know the value will be reassigned.
let: Used for variables whose values will change over time (block-scoped).
var: The older declaration keyword (function-scoped). Rarely used in modern ES6+ code.
Variables in JavaScript
// Declaring constants
const siteName = "Codexia";
const maxScore = 100;
// Declaring reassignable variables
let userXp = 40;
let isCompleted = false;
// Performing arithmetic
userXp += 25; // userXp is now 65
console.log("Welcome to", siteName);
console.log("Current XP:", userXp);| Data Type | Description | Example |
|---|---|---|
| String | Textual characters wrapped in quotes | "Hello World" |
| Number | Integers and floating point numbers | 42, 3.14 |
| Boolean | Logical true or false | true, false |
| Object | Key-value dictionary structure | { name: "John", age: 30 } |
| Array | Ordered list of items | [1, 2, 3, 4] |
| Undefined | Variable declared without a value | let x; |
| Null | Explicit representation of empty or nothing | null |
ADVERTISEMENT
Google AdSense Slot (inContentBanner)
Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which keyword should you use to declare a variable whose reference should NOT be reassigned?