1.4 Basic Data Types in JavaScript

JavaScript's data types are the building blocks for writing effective and efficient code.

Basic Data Types in JavaScript

Understanding data types in JavaScript is foundational for effective programming in the language. JavaScript, being a loosely typed and dynamic language, allows variable declarations without specifying their type. The type is determined automatically at runtime based on the variable's value. This flexibility is powerful but requires a solid understanding of the available data types to avoid unexpected behaviors. Here we will cover the basic data types in JavaScript, providing insights into their characteristics and their usage.

Primitive Data Types

Primitive data types in JavaScript are immutable values that cannot be altered once they are created. JavaScript offers seven primitive types:

1. String

Strings represent textual data. They are enclosed in single quotes ('), double quotes ("), or backticks (`). Backticks are used for template literals, which can span multiple lines and embed expressions.

let greeting = "Hello, world!";
let name = 'John Doe';
let message = `Welcome, ${name}`;

2. Number

The Number type represents both integer and floating-point numbers. JavaScript uses double-precision 64-bit binary format IEEE 754 values for numbers, meaning it does not differentiate between types of numbers.

let integer = 100;
let floatingPoint = 100.5;

3. BigInt

Introduced in ES2020, the BigInt type extends the Number type to represent whole numbers larger than 2^53 - 1, which is the largest number JavaScript can reliably represent with the Number type.

let largeNumber = 9007199254740991n;

4. Boolean

Boolean represents a logical entity and can have two values: true and false. It is often used in conditional statements to decide the flow of execution in programs.

let isJavaScriptFun = true;

5. Undefined

A variable that has not been assigned a value is of type undefined. It also represents the absence of a meaningful value.

let notAssigned;
console.log(notAssigned); // undefined

6. Null

null represents the intentional absence of any object value. It is often used to indicate that a variable should not point to any object.

let emptyValue = null;

7. Symbol

Introduced in ES6, the Symbol type is used to create unique identifiers for objects. Each created symbol is unique. Symbols are often used to add unique property keys to an object that won’t collide with keys any other code might add to the object, and which are hidden from any mechanisms other code will typically use to access the object.

let symbol1 = Symbol('symbol');

Non-Primitive Data Type

Object

The Object data type in JavaScript is used to store collections of data or more complex entities. Objects can be created using curly braces {}, with a list of properties. A property is a "key: value" pair, where the key is a string (also called a "property name"), and the value can be anything.

let person = {
    name: "John Doe",
    age: 30
};

Objects are mutable and can be changed after creation, making them versatile for various programming needs.


By understanding the characteristics and differences between these types, developers can make informed decisions about data manipulation and storage in their programs. Remember, JavaScript is a dynamically typed language, so it's essential to keep track of the type of data your variables are holding to avoid unexpected behaviors or bugs in your code.

Support us ❤️

Buy Us A Coffee