Javascript Data Types

JavaScript is a dynamically typed language, which means you don’t explicitly declare the data type of a variable when you create it. Instead, JavaScript determines the data type at runtime.

Easy Coding School summarize and describe the major types of data types: Primitive data types, non-primitive (reference) data types , and how to check data types in JavaScript.

1. Primitive Data Types:
Primitive data types in programming are fundamental data types that are not composed of other types. They are the basic building blocks for representing and manipulating data. In JavaScript, as well as in many other programming languages, primitive data types are used to store simple values. Here are the primitive data types in JavaScript:

Number: Represents both integer and floating-point numbers. For example: 42, 3.14.

// Number: Represents both integer and floating-point numbers
let score = 95;
let temperature = 28.5;

String: Represents a sequence of characters enclosed in single (”) or double (“”) quotes. For example: ‘Hello’, “World”.

// String: Represents a sequence of characters
let greeting = "Hello, world!";
let productName = 'Widget';

Boolean: Represents true or false values. For example: true, false.

// Boolean: Represents true or false values
let isActive = true;
let isLoggedIn = false;

Undefined: Represents a variable that has been declared but has not been assigned a value. For example: let x;.

// Undefined: Represents a variable declared but not assigned
let undefinedVar;
console.log(undefinedVar); // Outputs: undefined

Null: Represents the intentional absence of any value. For example: null.

// Null: Represents the absence of a value
let emptyValue = null;

Symbol: Represents a unique and immutable value, often used as property keys in objects.

// Symbol: Represents a unique value
const uniqueSymbol = Symbol('unique');
console.log(uniqueSymbol); // Outputs: Symbol(unique)

BigInt: Represents integers with arbitrary precision. For example: 1234567890123456789012345678901234567890n.

// BigInt: Represents integers with arbitrary precision
const bigNumber = 1234567890123456789012345678901234567890n;

Also read, JavaScript Topic-Based Learning Roadmap

2. Non-primitive Data Types (Reference Types):

Non-primitive data types, also known as reference types, are more complex data types in programming that can hold and reference other values. Unlike primitive data types, which hold the actual value, non-primitive data types store references to values. These references allow you to access and manipulate the actual data stored elsewhere in memory. In JavaScript, non-primitive data types include:

Object: Represents a collection of key-value pairs, often used to group related data together. For example: { name: ‘John’, age: 25 }.

// Object: Represents a collection of key-value pairs
let person = { name: 'John', age: 30 };

Array: Represents an ordered list of values enclosed in square brackets []. For example: [1, 2, 3].

// Array: Represents an ordered list of values
let numbers = [1, 2, 3, 4, 5];
let fruits = ['apple', 'banana', 'orange'];

Function: Represents a block of reusable code that can be executed when invoked.

// Function: Represents a block of reusable code
function greet(name) {
console.log(`Hello, ${name}!`);
greet('Alice'); // Outputs: Hello, Alice!/* Your code... */

Date: Date objects are used to work with dates and times. They provide methods for creating, manipulating, and formatting dates

// Date: Represents date and time values
const currentDate = new Date();
console.log(currentDate); // Outputs: Mon Aug 09 2023 10:00:00 GMT+0000 (Coordinated Universal Time)

Regular Expressions (RegExp): Patterns for string matching and manipulation. Example: /pattern/.

let regexPattern = /pattern/;

3. Checking Data Types:
You can use the typeof and instanceof operator to check the data type of a variable:

let value;
console.log(typeof value); // Outputs: "undefined"

value = "Hello";
console.log(typeof value); // Outputs: "string"

value = 42;
console.log(typeof value); // Outputs: "number"

let arr = [1, 2, 3];
console.log(typeof arr); // Outputs: "object"
console.log(arr instanceof Array); // Outputs: true

 Note: typeof is used to check primitive data types, while instanceof is used to check object types:

Script also offers more specialized data types, such as various objects like Map, Set, and Promise for more advanced programming concepts.

Note: We welcome your feedback at Easy coding School. Please don’t hesitate to share your suggestions or any issues you might have with the article!

5 thoughts on “Javascript Data Types

  1. I Just wanted to drop a quick note to say thank you for explaining data types in such a simple and easy-to-understand way. Your teaching style really helped me grasp the concepts, and I appreciate your efforts in making learning enjoyable. Thanks again!

Leave a Reply

Your email address will not be published. Required fields are marked *