Published
Data types in JavaScript determine the type of data that you can store and manipulate within a program. JavaScript is a dynamic type language, meaning you don't have to specify what kind of information will be stored in variable. For example, you can initially use a variable to store a number, and later use the same variable to store text.
JavaScript has seven basic data types or primitive types. They are Number, String, Boolean, Null, Undefined, Symbol, and BigInt.
The Number data type is used to represent positive or negative numbers with or without decimal place, or numbers written using exponential notation (e).
var num = 25; // A simple number
var flo = 80.5; // A number with a decimal point
var exp = 4e+2; // 400
The String data type is used for textual data. It must be enclosed in single or double quotes.
var str = 'Hello World';
The Boolean data type can only hold two values: true or false. It is typically used to store values like yes (true) or no (false), on (true) or off (false).
var isReading = true; // yes, I'm reading
var isSleeping = false; // no, I'm not sleeping
The Null data type has only one value: null. This means no value or no object. It implies no object, or null string, no valid boolean value, no number and no array object.
var emp = null;
A variable that has not been assigned a value is of type 'undefined'.
var und;
console.log(und); // undefined
Symbol is a new data type introduced in ECMAScript 2015. A value of type Symbol is created by invoking the function Symbol() which dynamically produces an anonymous unique value.
var sym = Symbol('Hello');
BigInt is a numeric data type that can represent integers in the arbitrary precision format. This is used when we need to represent integers that are too large (greater than 2^53-1).
var bigInt = 1234567890123456789012345678901234567890n;
In addition to primitive types, JavaScript also supports non-primitive types, namely Object, Array, and Function.
Objects are variables, but objects can contain many values. These values are written as a list of value pairs (name : value).
var person = {firstName:'John', lastName:'Doe'};
Arrays are special kind of objects that can hold more than one value at a time.
var colors = ['Red', 'Green', 'Blue'];
Functions are block of reusable code that perform a specific task. Functions are executed when they are called or invoked.
function greet() {
return 'Hello World';
}
greet(); // 'Hello World'
By reading this article, you've invested 1.75 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
JavaScript is like a magic language that helps to make websites more interactive and fun. For example, it can power games, animations, and surveys directly on a website.
Just like we have different kinds of toys like cars, balls, and dolls, JavaScript has different kinds of data. These types are: numbers (like 1 or 2), strings (like 'abc' or 'hello'), boolean (either true or false), null (means nothing), undefined (means we don’t know), object (like a toy box that can store many things), and symbol (a unique and cannot be changed).
This is like counting toys. We can add numbers, subtract them, or divide them. For example 5, -3, 0.12 are numbers.
let count = 5;
Strings are like words or sentences. They are always written inside quotes. For example 'Hello', 'abc', '123' are strings.
let word = 'Hello';
Booleans are like yes or no, true or false answers to a question. They are either true or false.
let isRaining = false;
let isSunny = true;
Null is when we have an empty box. It means there is nothing there.
Undefined is when we don't know what is inside the box. It means we haven't given it a value yet.
Objects are like toy boxes. They can hold several things at once. Each thing in the box has a name and a value.
Symbol is something unique and cannot be changed once it is created. Even if two symbols look the same, they are not the same.
By reading this article, you've invested 1.30 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
About author
Roman Y.
Senior Software Engineer at Nike
Certain articles only distributed to subscribers through email