JavaScript Basics: Variables, Data Types, and Operators Made Simple

![[JS 강좌] 2강 - 개발 환경 구축하기 (VS Code, Chrome 개발자 도구)](/uploads/editor/JS-%EA%B0%95%EC%A2%8C-2%EA%B0%95-%EA%B0%9C%EB%B0%9C-%ED%99%98%EA%B2%BD-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0-VS-Code-Chrome-%EA%B0%9C%EB%B0%9C%EC%9E%90-%EB%8F%84%EA%B5%AC-20260706-0dfe9b.webp)
What is a Variable?
The first concept you'll learn in programming is the variable.
A variable is simply a storage space for data.
For example, if you need to remember someone's name, age, or phone number, you must store it somewhere.
In JavaScript, you can store this data in variables and use it whenever needed.
You can write something like this:
let name = "Hong Gildong";
Here, name is the variable's name, and "Hong Gildong" is the data stored in the variable.
You can now use the value by referencing name anywhere in your program.
Why Use Variables?
Using variables allows you to reuse the same data multiple times.
For example, if a name changes, you only need to update the variable's value once.
let name = "Hong Gildong";
console.log(name);
console.log(name);
console.log(name);
Output
Hong Gildong
Hong Gildong
Hong Gildong
If the name changes, you only need to make one change in your code, making management much easier.
How to Declare Variables
In JavaScript, there are three ways to declare variables.
var age = 20;
let name = "Hong Gildong";
const country = "South Korea";
Let's look at the differences between them.
var
This is an older method that has been used for a long time.
While you can still use it, it's rarely used in professional settings today.
let
This is the most commonly used variable declaration method.
You can change its value later.
let score = 80;
score = 90;
console.log(score);
Output
90
const
You cannot change the value once it's stored.
const PI = 3.14;
If you try to change the value, an error will occur:
PI = 5;
In short,
Use
letfor values that may change.Use
constfor values that should not change.
Developing this habit will make your code more maintainable.
Rules for Naming Variables
You can create variable names freely, but there are some rules to follow.
Using meaningful variable names is important.
Good examples
userName
userAge
phoneNumber
totalPrice
Poor examples
a
abc
test1
x
Choose names that clearly indicate what kind of data they hold.
Data Types
Data types indicate what kind of data a variable stores.
The most common data types in JavaScript are:
Number
String
Boolean
Null
Undefined
Object
Array
We'll focus on the most commonly used data types in this lesson.
Number (Numbers)
This data type stores numeric values.
let age = 30;
let price = 15000;
let score = 98.5;
Both integers and decimals are treated as Number.
String (Text)
This data type stores text values.
You can use either double quotes or single quotes for strings.
let name = "Hong Gildong";
let city = 'Seoul';
If it's in quotes, it's a string even if it looks like a number.
let num = "100";
This value is a string, not a number.
Boolean (True or False)
A Boolean can only have two values:
true
false
For example:
let isLogin = true;
let isAdmin = false;
This is commonly used to represent login status or checkbox states.
Checking Data Types with typeof
You can use the typeof operator in JavaScript to check data types.
let age = 30;
console.log(typeof age);
Output
number
For strings:
let name = "Hong Gildong";
console.log(typeof name);
Output
string
Booleans will output boolean.
What are Operators?
Operators are symbols used to perform calculations or comparisons on data.
The most commonly used operators are arithmetic operators.
Arithmetic Operators
let a = 10;
let b = 3;
Addition
a + b
Result
13
Subtraction
a - b
Result
7
Multiplication
a * b
Result
30
Division
a / b
Result
3.333333...
Modulus (Remainder)
a % b
Result
1
Concatenating Strings
You can join strings using the + operator.
let first = "Hong";
let last = "Gildong";
console.log(first + last);
Result
HongGildong
You can also add spaces:
console.log(first + " " + last);
Comparison Operators
These operators compare two values.
10 > 5
Result
true
3 < 1
Result
false
Common comparison operators include:
Operator | Meaning |
|---|---|
== | Checks if values are equal |
=== | Checks if values and data types are both equal |
!= | Checks if values are not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
In professional settings, it's recommended to use === instead of ==.
Increment and Decrement Operators
These operators increase or decrease a value by 1.
let count = 1;
count++;
console.log(count);
Output
2
To decrease, use:
count--;
Assignment Operators
You can perform calculations while assigning values.
let score = 50;
score += 10;
Output
60
This is equivalent to:
score = score + 10;
You can also use other assignment operators like -=, *=, and /=.
Key Takeaways from This Lesson
Today, you learned the most fundamental JavaScript syntax.
The key points to remember are:
A variable is a storage space for data.
letallows values to be changed later.constprevents value changes after initialization.Number, String, and Boolean are the most commonly used data types.
You can check a variable's data type using
typeof.Operators are used for calculations and comparisons.
In professional settings, use
===instead of==.
Conclusion
Variables and data types form the foundation for all future JavaScript concepts.
While they may seem simple at first, everything from conditionals and loops to functions, objects, DOM manipulation, and AJAX relies on these fundamentals.
Try entering the examples from this lesson, change values, and see how results differ. Hands-on practice is the best way to learn.
In our next lesson, we'll explore conditional statements (if, switch) and loops (for, while) to build more complex functionality.
FAQ
What is the difference between let and const?
let allows you to change a variable's value later, while const prevents changes after initialization.
Why should I use === instead of == in JavaScript?
The === operator checks both value and data type, making it more reliable than == which only checks value.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [JS 강좌] 3강 - 변수, 자료형, 연산자 쉽게 이해하기