In Programming, Data Types are the most important for defining variables, in any other programming language for storing different data types we have different things but in JS they can hold any type of data.
That's why Javascript is called "Loosely Type Language"
Javascript has mainly 8 Datatypes -
String
Number
Boolean
Undefined
Null
BigInt
Symbol
Object
Let's see how to store these data types in Javascript
String-
It represents the sequence of characters.
let greeting = "Hello, World!"; let name = 'John Doe';
Number-
It represents numeric values, including integers and floating-point numbers.
let integerNumber = 42; let floatingPointNumber = 3.14;
Boolean -
It represents a binary value, either
true
orfalse
.let isProgrammingFun = true; let isRainyDay = false;
Undefined -
It represents a variable that has been declared but not assigned a value.
let a; console.log(a); // Output: undefined
Null -
It represents the intentional absence of any object value.
let Value = null;
BigInt -
It was introduced in ECMAScript 2020, It is used for representing integers of arbitrary precision.
let storeBigVal= 1885699254740991n;
Symbol -
It's Introduced in ECMAScript 6, symbols are unique and immutable data types
const sampleSymbol = Symbol("description");
Object -
It represents a collection of key-value pairs and is a fundamental data structure in JavaScript.
let Person = { name: "Saurav", age: 20, isStudent: true };
Type Coercion
Type coercion is the automatic conversion of one data type to another in a programming language.
JavaScript, being a loosely typed language, performs type coercion in certain situations, allowing operations between different data types without explicit conversion by the developer.
let num = 10; // a number
let str = "5"; // a string containing a number
let result = num + str;
console.log(result); // Output: 105
In this example, We have a number (num) and a number-as-text (str). When we use '+' in JavaScript, it turns the text into a number, but it also works as a joiner. So, instead of adding, it combines them, resulting in '105'.
Type of Operator
The typeof
operator in JavaScript helps you find out the data type of a value or variable. It's like asking, "What kind of thing is this?"
let num = 42;
console.log(typeof num); // Output: "number"
Here, typeof
tells us that the data type of num
is a "number.
Thanks for Checking this, Happy Coding ๐