Variables

Store values and understand common JavaScript data types.

Variables and data types are the foundation of JavaScript programming. Before a program can perform calculations, store information, or make decisions, it needs a way to store data. This is where variables and data types become important.

Think of a variable as a labeled container that stores information. Just like you might store different items in different boxes at home, JavaScript stores different kinds of data in variables.

What Is a Variable?

A variable is a named storage location used to hold data. The stored value can be changed or updated while the program is running.

For example, if you are building a shopping website, you might need to store a customer's name, age, or the total price of products in their cart.

In JavaScript, variables can be created using let, const, or var.

javascript
let name = "John";
let age = 25;

In this example:

  • name stores the value John.
  • age stores the value 25.

Declaring Variables

Using let

The let keyword is used when the value may change later.

javascript
let score = 100;

score = 150;

console.log(score);

Output:

javascript
150

Using const

The const keyword is used when the value should not change.

javascript
const country = "India";

console.log(country);

Once a value is assigned to a const variable, it cannot be reassigned.

Using var

The var keyword was used in older versions of JavaScript. Modern developers generally prefer let and const because they are easier to manage and less likely to cause unexpected behavior.

javascript
var city = "Delhi";

What Are Data Types?

A data type tells JavaScript what kind of information is being stored in a variable.

Different types of data require different handling. For example, text is treated differently from numbers.

Common JavaScript Data Types

String

A string represents text.

javascript
let message = "Welcome to JavaScript";

Strings are enclosed in single quotes or double quotes.

Number

Numbers are used for mathematical calculations.

javascript
let price = 499;
let rating = 4.5;

JavaScript uses the same data type for both whole numbers and decimal numbers.

Boolean

A boolean can have only two values: true or false.

javascript
let isLoggedIn = true;

Booleans are commonly used for conditions and decision-making.

Undefined

A variable that has been declared but not assigned a value is undefined.

javascript
let username;

console.log(username);

Output:

javascript
undefined

Null

null represents an intentional empty value.

javascript
let selectedProduct = null;

This means the variable currently has no value assigned.

Checking a Data Type

JavaScript provides the typeof operator to check the type of data stored in a variable.

javascript
let age = 25;

console.log(typeof age);

Output:

javascript
number

Real-World Example

Imagine an online shopping website.

javascript
let customerName = "Sarah";
let totalAmount = 1499;
let isMember = true;

Here:

  • customerName is a string.
  • totalAmount is a number.
  • isMember is a boolean.

Each variable stores a different type of information, allowing the website to function correctly.

Summary

Variables allow JavaScript programs to store and manage information. They act like containers that hold values used throughout a program. Data types define the kind of data stored in those variables, such as text, numbers, or true/false values. Understanding variables and data types is essential because they are used in almost every JavaScript program, from simple calculators to complex web applications.

Let's learn with DevBrainBox AI