- 01Setting Up (What You'll Need)
- 02Creating Variables
- 03Conditionals (if Statements)
- 04Loops (for Statements)
- 05Creating Functions
- 06Working with Arrays
- 07Working with Objects
- 08Loops (while Statements)
- 09Working with Strings
- 10Using Classes (Object-Oriented Programming)
- 11Error Handling (try...catch)
- 12Destructuring and the Spread Syntax
- 13Async Code (Promise / async & await)
- 14Advanced Array Methods (filter and reduce)
- 15The switch Statement
- 16The Ternary Operator
- 17Inheritance (Extending Classes)
- 18How to Write Comments
- 19Logical Operators (AND, OR, NOT)
- 20Constants (Making Read-Only Values with const)
- 21Splitting and Joining Strings (split and join)
- 22Null-Safe Syntax (?? and ?.)
- 23Searching Arrays and Collections (includes and find)
- 24Transforming Arrays with map
- 25Two-Dimensional Arrays (Table-Shaped Data)
- 26Building a Custom Error Class
- 27Default Arguments (Setting Initial Values for Parameters)
- 28Using Set (Collections)
- 29Verifying Correctness with assert (Your First Step Into Testing)
- 30Higher-Order Functions (Passing a Function as an Argument)
- 31Stacks and Queues (Basic Data Structures)
- 32The Basics of Type Conversion (Casting)
- 33Introduction to Regular Expressions (Pattern Matching)
- 34The Binary Search Algorithm
- 35Building a Caesar Cipher (a Letter-Shifting Cipher)
- 36Understanding How Bubble Sort Works
- 37Building and Displaying Dates (Basic Year/Month/Day Operations)
- 38Writing Several Unit Tests Together (Multiple Test Cases)
- 39Speeding Up Calculations with Memoization (Caching)
- 40Normalizing Strings (trim and Case Unification)
- 41The Difference Between Shallow Copy and Deep Copy
- 42The Basics of Enums (Enumerated Types)
- 43Flattening Arrays (flatten)
- 44Reversing a String and Checking for a Palindrome
- 45Pairing Up Two Arrays (a zip Operation)
- 46Rounding Numbers (floor, ceil, and round)
- 47Multi-Line Strings (Template Literals)
- 48Returning Multiple Values from a Function (Array Destructuring)
- 49Finding the GCD and LCM (the Euclidean Algorithm)
- 50Formatting Numbers (Digit Alignment and Decimal Precision)
- 51Cleanup Processing with try/catch/finally
- 52Writing Type-Agnostic, General-Purpose Functions
- 53The Basics of Map (an Object for Key-Value Pairs)
- 54Generating Random Numbers
- 55Bitwise Operations (AND, OR, XOR, and Shift Operations)
- 56Using static (Static Class Properties and Methods)
- 57Waiting a Fixed Amount of Time (setTimeout and await)
- 58Watch Out for Floating-Point Rounding Error
- 59Transforming and Flattening at Once with flatMap()
- 60FizzBuzz (the Classic Practice Problem)
- 61Checking Whether a Number Is Prime
- 62Set Operations with Set (Union, Intersection, and Difference)
- 63Converting Number Bases (Binary and Hexadecimal)
- 64Checking That Brackets Match (an Application of Stacks)
- 65Checking for an Anagram
- 66Checking Whether a Year Is a Leap Year
- 67Converting Temperature (Celsius ⇄ Fahrenheit)
- 68Finding the Prime Factorization
- 69[Applied] Build a Simple To-Do List Tool
Creating Variables
In this lesson you'll learn how to declare variables in JavaScript from scratch, understand the difference between let and const, and become able to use them correctly in your own code. This is written for anyone who searched "JavaScript variables tutorial" and landed here, so we'll start from the basics.
A variable is like a "box" that holds a value under a name. In JavaScript you declare a variable with let or const. let variableName = value; creates a variable whose value can be changed later, while const variableName = value; creates a variable whose assigned value can never be changed (a constant). A good beginner rule of thumb is "use let for values that might change, const for values that won't" — and in real projects, the recommended style is to "start with const, and only switch to let when you actually need to reassign."
The sample code declares two variables, let name and const age, and displays them with console.log(). Joining strings with + ("Hello, " + name + "!") is the basic technique for combining several strings and variable values into one message. Note that when a numeric variable (age) is concatenated with a string, it's automatically converted to a string — this becomes useful later when you combine variables with conditionals and loops.
A common stumbling block for beginners is reassigning a variable declared with const and getting an "Assignment to constant variable" error. This happens because you're trying to overwrite a variable you promised not to change — once you understand the cause, it's nothing to fear. Other common pitfalls include using a variable before it's declared, or declaring the same variable name twice with let, both of which cause errors.
In real development, all kinds of information — a user's input, data fetched from an API — first gets stored in a variable before it's processed. Values reused in multiple places on screen, like the logged-in user's name or the total price of items in a cart, are typically managed as variables. How you declare and name variables directly affects how readable and maintainable your code is, which is why it's taken seriously even in code review.
Naming matters too: choosing a name like userName or totalPrice that hints at its contents, rather than a single letter like n, is strongly encouraged in real-world code. Clear variable names make a huge difference in how quickly someone (including future you) can understand the code later.
💡 Anything passed to console.log() appears in the output below.
