Library
LearnJavaScriptChapter 29
4 min read

Variables and Data Types in JavaScript

In JavaScript, variables and data types are fundamental concepts that every beginner needs to understand. They are the building blocks of any program, allowing you to store and manipulate data. Let's break down what variables are, how to declare them, and the different types of data you can work with.


1. What Are Variables?#

A variable is like a storage container that holds data. You can store various types of information inside variables, such as numbers, strings, and more. Variables in JavaScript are used to reference and manipulate data throughout your program.

2. Declaring Variables#

There are three primary ways to declare a variable in JavaScript: var, let, and const.

2.1 var#

var was the original way to declare variables in JavaScript. However, it has some quirks, such as function scoping (meaning a variable declared with var inside a function is accessible throughout the entire function). It's now recommended to avoid using var unless necessary.

Example:

javascript
var name = "John";
console.log(name); // "John"

2.2 let#

let is a newer way to declare variables and provides block-level scoping. This means a variable declared with let is only accessible within the block (usually enclosed in curly braces {}) in which it is defined.

Example:

javascript
let name = "Jane";
console.log(name); // "Jane"

2.3 const#

const is used to declare variables whose values cannot be reassigned. It's a good practice to use const for values that you don't want to change, like configurations or constants. However, note that const does not make the value immutable; for example, objects and arrays declared with const can still have their contents modified.

Example:

javascript
const pi = 3.14;
console.log(pi); // 3.14
// pi = 3.14159; // Error: Assignment to constant variable

3. Data Types in JavaScript#

JavaScript supports a variety of data types, each used for different kinds of values. The main data types in JavaScript are divided into two categories: Primitive Types and Non-Primitive Types.

3.1 Primitive Data Types#

Primitive types are the most basic types of data in JavaScript. They are immutable (meaning their values cannot be changed once they are set). The primitive data types in JavaScript are:

  1. String A string is a sequence of characters, enclosed in single, double, or backtick quotes.

    Example:

    javascript
    let name = "Alice";
    let greeting = 'Hello, ' + name;
    console.log(greeting); // "Hello, Alice"
  2. Number Numbers in JavaScript can be integers or floating-point numbers.

    Example:

    javascript
    let age = 25;
    let price = 99.99;
    console.log(age);  // 25
    console.log(price); // 99.99
  3. Boolean A boolean represents a logical value that can either be true or false.

    Example:

    javascript
    let isActive = true;
    let isRaining = false;
    console.log(isActive); // true
  4. Undefined A variable is undefined if it has been declared but has not yet been assigned a value.

    Example:

    javascript
    let score;
    console.log(score); // undefined
  5. Null null is an intentional assignment of "no value" or "empty." It’s often used to represent the absence of an object.

    Example:

    javascript
    let user = null;
    console.log(user); // null
  6. Symbol (ES6+) A symbol is a unique and immutable primitive value. It's often used to create unique object properties.

    Example:

    javascript
    const symbol = Symbol('unique');
    console.log(symbol); // Symbol(unique)
  7. BigInt (ES11+) BigInt is a data type that can represent integers larger than the number limits of the Number type.

    Example:

    javascript
    const bigNumber = BigInt(1234567890123456789012345678901234567890);
    console.log(bigNumber); // 1234567890123456789012345678901234567890n

3.2 Non-Primitive Data Types#

Non-primitive data types are more complex and are mutable. These types include:

  1. Object Objects are collections of properties and methods, and they can store multiple values as key-value pairs. Objects are used for more complex data structures.

    Example:

    javascript
    const person = {
      name: "John",
      age: 30,
      greet: function() {
        console.log("Hello, " + this.name);
      }
    };
    console.log(person.name);  // "John"
    person.greet(); // "Hello, John"
  2. Array Arrays are ordered collections of values, which can be of any type. You can access array elements using an index.

    Example:

    javascript
    const colors = ["red", "green", "blue"];
    console.log(colors[0]); // "red"
    colors.push("yellow");
    console.log(colors); // ["red", "green", "blue", "yellow"]

4. Type Conversion#

Sometimes you may need to convert one data type to another. JavaScript provides several ways to convert values from one type to another, such as using String(), Number(), Boolean(), etc.

4.1 String Conversion#

javascript
let num = 100;
let str = String(num);
console.log(str); // "100"
console.log(typeof str); // "string"

4.2 Number Conversion#

javascript
let str = "123";
let num = Number(str);
console.log(num); // 123
console.log(typeof num); // "number"

4.3 Boolean Conversion#

javascript
let val = 0;
let boolVal = Boolean(val);
console.log(boolVal); // false

5. Type Checking#

To check the type of a variable, you can use the typeof operator. For objects (including arrays), typeof will return "object," so to check if an object is an array, you can use Array.isArray().

Example:

javascript
let num = 42;
let str = "Hello, World!";
let arr = [1, 2, 3];
 
console.log(typeof num); // "number"
console.log(typeof str); // "string"
console.log(Array.isArray(arr)); // true

6. Summary#

  • Variables store data in JavaScript and can be declared using var, let, or const.
  • Primitive Data Types include String, Number, Boolean, Undefined, Null, Symbol, and BigInt.
  • Non-Primitive Data Types include Object and Array.
  • JavaScript allows for type conversion and provides tools like String(), Number(), and Boolean() for this.
  • Use typeof and Array.isArray() to check the type of a variable.

Practice Code Snippet#

Here’s a practical example for you to try out:

Live code runner

Operators