Javascript Data Types


Jan 01 2024

Programming languages categorize data based on its type, size, and the values it can hold, which is known as data types. Each programming language has its specific set of data types, and how data is stored and manipulated varies according to these data types.

In this article, we will explore the different data types in JavaScript.

There are six primitive data types in JavaScript:

  • Number
  • String
  • Boolean
  • Null
  • Undefined
  • Symbol

In addition to these primitive types, there is also an object data type. Let's discover the first one, number

Number

The number data type represents numeric values, both integers and floating-point numbers. Numbers can be positive or negative, and can be expressed in scientific notation. For example:

let a = 42;
let b = 3.14159;
let c = 1.23e-6;

String

The string data type represents a sequence of characters. Strings are enclosed in single or double quotes. For example:

let greeting = "Hello, world!";
let name = 'Alice';

Boolean

The boolean data type represents a logical value, either true or false. For example:

let isHappy = true;
let isSunny = false;

Null

The null data type represents a deliberate non-value. It is often used to represent the absence of an object or value. For example:

let a = null;

Undefined

The undefined data type represents an uninitialized value. When a variable is declared but not assigned a value, it is assigned the value undefined by default. For example:

let a;
console.log(a); // Output: undefined

Symbol

The symbol data type was introduced in ECMAScript 6. Symbols are unique and immutable values that can be used as object property keys. The main advantage of symbols is that they are unique and cannot be accidentally overwritten or accessed by other parts of the code. For example:

const key1 = Symbol();
const key2 = Symbol();
console.log(symbol1 === symbol2); // Output: false

In addition these primitive types in JavaScript, there is also a Non-primitive data type, object:

Object

The object data type represents a collection of key-value pairs, where the key is a string and the value can be of any data type, including another object. Objects are created using curly braces. For example:

let person = {
  name: 'Alice',
  age: 30,
  isStudent: true
};

Conclusion

In conclusion, understanding the different data types in JavaScript is important for writing efficient and bug-free code. In addition to the six primitive types (number, string, boolean, null, undefined, and symbol), there is also the object type. By understanding the data types and how they are used in JavaScript, you can write better code that is easier to read and maintain.