Back to List

JavaScript Variables and Data Types

Understand how to declare variables and the different data types in JavaScript.

Beginner
|
5min
|
Verified (2026-07)
Progress0/55 (0%)

JavaScript Variables and Data Types

After Completing This Topic

You will be able to declare variables using let and const, and you will be able to distinguish the 6 basic data types in JavaScript.


What is a Variable?

A variable is a named container for values. You can store a value and then retrieve it later.

javascript
let userName = "Kim Developer";
let age = 28;

console.log(userName); // "Kim Developer"
console.log(age);      // 28

// Variables declared with `let` can have their values changed.
age = 29;
console.log(age); // 29

There are three keywords for declaring variables in JavaScript:

javascript
let count = 0;     // Variable whose value can change.
const PI = 3.14;   // Constant whose value cannot change.
var old = "Old";   // Legacy method β€” not used anymore.

Just remember let and const. var has scope issues (the range in which a variable is valid), so it is not used in modern JavaScript. Use const for values that don't need to be changed, and let for values that need to be changed.

6 Data Types

Values in JavaScript have a type. Numbers and strings are treated differently.

javascript
// 1. String β€” text enclosed in quotes.
const name = "Hong Gildong";
const greeting = 'Hello';

// 2. Number β€” no distinction between integers and decimals.
const score = 95;
const ratio = 0.85;

// 3. Boolean β€” true or false.
const isLoggedIn = true;
const hasError = false;

// 4. null β€” used to intentionally represent "no value".
const selected = null;

// 5. undefined β€” a state where a value has not yet been assigned.
let result;
console.log(result); // undefined

// 6. Object β€” used to store multiple values together.
const user = {
  name: "Kim Developer",
  age: 28,
  active: true
};
console.log(user.name); // "Kim Developer"

Checking the Type with typeof

If you don't know what type a value has, you can use typeof. This is useful for debugging when the type in AI-generated code is different from what you expected.

javascript
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" ← A famous bug in JavaScript.

// Number + string = concatenated as a string (be careful!).
console.log(10 + "5");  // "105" (not the number 15!)
console.log(10 - "5");  // 5    (subtraction converts to a number)

The fact that typeof null returns "object" is a bug that has existed since the early days of JavaScript. It has not been fixed yet. Also, 10 + "5" results in "105" β€” this type coercion is one of the most common causes of errors in JavaScript.


β†’ Apply to bio: DevBench β€” Python Fundamentals

πŸ’¬ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...