JavaScript Data Types

JavaScript Data Types

·

7 min read

Introduction 🧾

JavaScript is a powerful and versatile programming language that can be used for various purposes such as web development, data analysis, game development, and more. However, to master JavaScript, you need to understand the basics of its data types. Data types are the building blocks of any programming language. They define the kind of values that variables can hold and how they can be manipulated. In this blog post, I will explain the different data types in JavaScript and show you some examples of how to use them. Let’s get started!

Primitive Data Types 💻

Primitive data types are the simplest and most basic data types in JavaScript. They are also called in-built or predefined data types because they are provided by the language itself. There are seven primitive data types in JavaScript:

1) String

A string is a sequence of characters that represents text. For example, "Hello" and 'World' are strings. You can use single or double quotes to create strings, but they must match at the beginning and end of the string. Strings can contain any characters, including numbers, symbols, spaces, and even other quotes (as long as they are different from the ones surrounding the string). You can use the + operator to concatenate (join) strings together. For example, "Hello" + " " + "World" will produce "Hello World".

2) Number

A number is a numerical value that can be an integer or a floating-point number. For example, 42 and 3.14 are numbers. JavaScript uses the same format to store all numbers, regardless of whether they have decimals or not. This format is called double-precision 64-bit binary format IEEE 754, which can represent numbers up to about 10^308 and down to about 10^-324. However, this also means that some numbers cannot be represented exactly and may lose some precision. For example, 0.1 + 0.2 will not give 0.3, but 0.30000000000000004. You can use the arithmetic operators (+, -, *, /, and %) to perform calculations with numbers. For example, 2 + 2 will give 4 and 10 / 2 will give 5.

3) Boolean

A boolean is a logical value that can be either true or false. For example, true and false are booleans. Booleans are often used to represent the outcome of comparisons or conditions. For example, 2 > 1 will give true and 2 == 1 will give false. You can use the logical operators (&&, ||, and !) to combine or negate booleans. For example, true && false will give false and !true will give false.

4) Null

A null is a special value that represents the absence of any value. For example, null is a null. Null is often used to indicate that a variable has no value or that an object has no property. For example, let x = null; will declare a variable x and assign it the value null. You can use the === operator to check if a value is null. For example, x === null will give true.

5) Undefined

An undefined is a special value that represents the state of a variable that has not been initialized or assigned a value. For example, undefined is an undefined. Undefined is the default value of any variable that is declared but not initialized. For example, let y; will declare a variable y but not assign it any value, so its value will be undefined. You can use the === operator to check if a value is undefined. For example, y === undefined will give true.

6) BigInt

A BigInt is a numerical value that can represent integers larger than 2^53 - 1 or smaller than -2^53 + 1, which are the limits of the number data type. For example, 900719925124740999n and -900719925124740999n are BigInts. BigInts are created by appending n to the end of an integer literal or by calling the BigInt() function. For example, let z = 900719925124740999n; or let z = BigInt(900719925124740999); will create a BigInt variable z. You can use the arithmetic operators (+, -, *, /, and %) to perform calculations with BigInts, but you cannot mix them with numbers. For example, z + 1n will give 900719925124741000n but z + 1 will give an error.

7) Symbol

A symbol is a unique and immutable value that can be used as a property key of an object. For example, Symbol('foo') and Symbol('bar') are symbols. Symbols are created by calling the Symbol() function with an optional description. For example, let s = Symbol('foo'); will create a symbol variable s with the description 'foo'. Symbols are guaranteed to be unique, even if they have the same description. For example, Symbol('foo') === Symbol('foo') will give false. You can use the [] notation to access or assign a property of an object using a symbol. For example, let obj = {}; obj[s] = 'Hello'; will create an object obj and assign it a property with the key s and the value 'Hello'.

Non-Primitive Data Types

Non-primitive data types are the complex and derived data types in JavaScript. They are also called reference or object data types because they are based on the object data type. There is only one non-primitive data type in JavaScript:

1) Object

An object is a collection of key-value pairs that can store different types of data. For example, {name: 'John', age: 25} is an object. Objects are created by using curly braces {} or by calling the Object() function. For example, let person = {name: 'John', age: 25}; or let person = new Object(); person.name = 'John'; person.age = 25; will create an object variable person. You can use the . notation or the [] notation to access or assign a property of an object. For example, person.name or person['name'] will give 'John' and person.age = 26; or person['age'] = 26; will change the value of the age property to 26.

Objects are very useful and flexible data types in JavaScript. They can be used to model real-world entities, such as users, products, animals, etc. They can also be used to create other data types, such as arrays, dates, functions, etc. For example:

  • Array

    An array is a special kind of object that can store multiple values in a single variable. For example, [1, 2, 3] is an array. Arrays are created by using square brackets [] or by calling the Array() function. For example, let numbers = [1, 2, 3]; or let numbers = new Array(1, 2, 3); will create an array variable numbers. You can use the index (starting from 0) to access or assign an element of an array. For example, numbers[0] will give 1 and numbers[1] = 4; will change the second element to 4.

  • Date

    A date is a special kind of object that can store a date and time value. For example, new Date() is a date. Dates are created by calling the Date() function with or without the new keyword. For example, let today = new Date(); or let today = Date(); will create a date variable today with the current date and time. You can use the methods of the date object to get or set different parts of the date and time. For example, today.getFullYear() will give the current year and today.setHours(10) will set the hours to 10.

  • Function

    A function is a special kind of object that can perform a specific task. For example, function add(a, b) { return a + b; } is a function. Functions are created by using the function keyword or by using the arrow syntax =>. For example, let add = function(a, b) { return a + b; }; or let add = (a, b) => a + b; will create a function variable add. You can use the () notation to call or invoke a function with arguments. For example, add(2, 3) will give 5.

Conclusion

In this blog post, I have explained the different data types in JavaScript and how to use them. I hope you have learned something new and useful. Data types are the foundation of any programming language, and knowing them well will help you write better and more efficient code. If you have any questions or feedback, please leave a comment below. Thank you for reading! 😊

Â