JavaScript, at its core, works with variables and data types just as binary code does with just 1s and 0s. These are quite essential for understanding and manipulating information within our code. In this article, we will be focusing on the various data types in JavaScript.
What are Data Types
It's as simple as it sounds-- Data types are the "Types of data".
As defined in Computer Science, it is a classification that defines the types of values variables can hold and how the computer should interpret them. In JavaScript, these data types could be classified into 2 main groups:
- Primitive
- Non-Primitive
LET'S GET INTO IT!
Primitive data types
These are simple and immutable, meaning their value cannot be changed after creation. There are 7 primitive data types in JavaScript:
- String
- Number
- Boolean
- Undefined
- Null
- Symbol
- BigInt
String
The string type is used to represent a sequence of characters, which could be names or special messages. They are declared using single or double quotes or backticks. Strings have some properties, such as indexability and immutability, meaning that each character in a string can be accessed by its numerical index (starting from 0).
let name = "Claire";
let age = "5";
console.log(name); // Claire
console.log("My name is " + name + "." + " I am " + age); // My name is Claire. I am 5
You can On the last line of code, we have a + character used after the string; this can be used to join another value to your string -- It's called Concatenation.
Number
The number type is used to represent both integer(whole number) and floating-point numbers (decimal). They can be used to perform various mathematical operations.
There is a range of numbers in JavaScript that is defined by Numbers.MAX_VALUE and Numbers.MIN_VALUE, which represents the largest and smallest numbers. Outside the range of what's possible, JavaScript offers some Special number values like:
- Infinity: A positive number greater than the maximum value in JavaScript
- -Infinity: A negative number less than the minimum value in JavaScript
- NAN: This means Not a Number. Represents a computational error for when an operation cannot be expressed as a number.
let subject1 = 12;
console.log(subject1) // 12
let subject2 = 1.3;
console.log(subject2) // 1.3
let result = 12 / 0;
console.log(result) // Infinity
let subject3 = 'Claire' / 2;
console.log(subject3) // NAN
Boolean
The boolean type represents two possible values: True or False.
let isStudent = true;
console.log(isStudent) // true
let isStudentRich = false;
console.log(isStudentRich) // false
Guiding rules for Boolean:
- Strings: An empty string ("") will be converted to false, and any other string will be converted to true.
- Numbers: Zero (0) and NaN (Not a Number) will be converted to false, and any other number will be converted to true.
- Objects: All objects, including arrays and functions, will be converted to true.
- Undefined: Undefined will be converted to false.
Undefined
The undefined type represents a variable that has been declared but not assigned a value. It is used to indicate the absence of a value
let webBridge;
console.log(webBridge) //undefined
Null
The Null type is used to represent an empty or unknown value. This is used intentionally
let money = null;
console.log(money) //null
Symbol
This is a unique value used as an identifier for object properties. They help create unique values in objects, preventing conflicts with other properties.
let name = Symbol("Claire");
let name2 = Symbol("Claire");
console.log(name == name2); //false
You can see this code has the same values passed to it, but they do not equal each other.
BitInt
BigInt is a type that represents a whole number greater than the JavaScript maximum integer
let b = BigInt("0b1010101001010101001111111111111111");
console.log(b); // 11430854655n
Non-Primitive Types
There are complex data structures that hold multiple values and more complex data. Unlike primitive types that directly store actual values, non-primitive types store references to the values.
Object:
An object is a data structure used to store data in key-value pairs. They are the fundamental blocks of JavaScript, as nearly everything used in the language is an object-- even functions.
let web3Bridge = {
type: "Web3 Advance",
name: "Claire"
}
console.log(web3Bridge.type) // Web3 Advance
Arrays
An array is a special type of object used for storing an ordered collection of values, usually indexed by numbers.
let members = ["Claire", "Joy", "Emmanuel"];
console.log(members); // ["Claire", "Joy", "Emmanuel"]
Functions
A function is a reusable block of code designed to perform a particular task.
function addNumbers (num1, num2) {
return num1 + num2;
}
console.log(addNumbers(4, 4)); // 8
console.log(addNumbers(3, 8)); // 11
Top comments (0)