Understanding Variables and Data Types

Learn the fundamentals of JavaScript variables and data types and how to use them.

🎯 Introduction

Variables and data types are among the most fundamental concepts in JavaScript. Understanding them helps you write correct, efficient code. This article explains what variables are, which data types are available, and how to use them in code. It is useful both for beginners and for developers reviewing the basics.

🔥 What Is a Variable?

A variable is a container for data. Think of it as a box that holds a value: you access that value using the variable’s name.

Before ES6, JavaScript used var to declare variables. ES6 introduced let and const, but this article uses only var to focus on pre-ES6 syntax.

To declare a variable:

var variableName;

To assign a value:

variableName = value;

You can combine declaration and assignment:

var variableName = value;

For example:

var name = 'Alex';
var age = 18;

Variable names follow these rules:

  • They can contain letters, digits, underscores, and dollar signs $
  • They cannot start with a digit
  • They cannot be JavaScript reserved words, such as var or function
Choose meaningful variable names to make your code easier to understand and maintain.

🧩 What Are Data Types?

A data type describes the kind of value stored in a variable. For this introduction, JavaScript values fall into two broad groups:

  1. Primitive values (simple values)
  2. Reference values (objects)

1. Primitive Values

  • String: Text enclosed in single or double quotes, such as "hello" or 'world'
  • Number: Numbers, including integers and decimals, such as 10 and 3.14
  • Boolean: A true or false value, with only two possibilities: true and false
  • null: Represents an intentional absence of a value
  • undefined: The value of a variable that has been declared but not assigned a value
  • NaN: A special number value meaning “not a number,” often produced by an invalid mathematical operation

2. Reference Values

  • Object: A data structure that stores key-value pairs
  • Array: A special kind of object that stores an ordered collection of values
  • Function: An object that can execute a block of code
JavaScript is dynamically typed: a variable can hold values of different types over time.

🔧 Using Variables and Data Types

Declaring and Assigning Variables

var city = 'Beijing';
var temperature = 26;
var isSunny = true;
var data = null;
var notDefined;

Storing and Updating Data

var message = 'The weather is nice';
message = 'The weather has turned cooler';

Operations with Different Data Types

  • Concatenating strings and numbers:
var name = 'Sam';
var age = 20;
var info = name + ' is this many years old: ' + age;
  • Adding, subtracting, multiplying, and dividing numbers:
var a = 10;
var b = 3;
var sum = a + b; // 13
var difference = a - b; // 7
var product = a * b; // 30
var quotient = a / b; // 3.3333...

Checking a Value’s Type

Use the typeof operator to check a value’s type:

var name = 'jack';
typeof name; // "string"
typeof 123; // "number"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" (a historical JavaScript quirk)
typeof {}; // "object"
typeof []; // "object"
typeof function () {}; // "function"
Be aware that typeof null returns "object". This is a historical quirk in JavaScript.

❓ Knowledge Check

Q1: Which keyword declares a variable?

When using pre-ES6 syntax, declare variables with var.

Q2: Which primitive types does JavaScript have in this introduction?

String, number, boolean, null, and undefined.

Q3: What does typeof null return, and why?

It returns "object" because of a historical quirk in JavaScript.

Q4: What kinds of data can a variable store?

A variable can store values of any type, including primitive and reference values.

🧪 Exercises

Exercise 1: Declare and Assign Variables of Different Types

Declare five variables containing a string, a number, a boolean, null, and undefined, respectively.

var str = 'Front-end';

// Declare the remaining four variables here

Exercise 2: Check and Display a Value’s Type

Declare a variable and assign it a value. Use typeof to check its type, then display the result with alert.

var value = 50;

// Write your code here

alert('The variable type is: ' + type);

🎉 You now understand the basics of JavaScript variables and data types! Apply these concepts to manage and manipulate data in your next program.