Data types in JavaScript you must know...

A Javascript Developer from India.
Hello coders... Welcome back again. Giving some time to Programming is a really pleasant thing. Fortunately, I got this thing in my later thirties. Currently learning these things and yes, learning has no limits. Ok... Let's move on to our topic. Today here I am going to write about the Data types in Javascript. So let's start...
Data types
JavaScript is a dynamically typed language. It means that a variable doesn’t associate with a type. In other words, a variable can hold a value of different types. For example:
let age = 28; // age is a number
graguate = true; // graduate is now a boolean
firstName = "Subodh"; // firstName is now a string
So in the above example, we declare the values as different datatypes. There are Eight basic data types in JavaScript. Here, we’ll cover them in general. JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.
Primitive data type
Non-primitive (reference) data type
JavaScript Primitive data types
There is 6 type of primitive data types in Javascript. Let's talk about them in detail.
Number
let n = 123;
n = 12.345;
The number type represents both integer and floating point numbers.
There are many operations for numbers, e.g. multiplication *, division /, addition +, subtraction -, and so on.
Besides regular numbers, there are so-called “special numeric values” which also belong to this data type: Infinity, -Infinity and NaN.
Infinityrepresents the mathematical Infinity ∞. It is a special value that’s greater than any number.We can get it as a result of division by zero:
alert( 1 / 0 ); // InfinityOr just reference it directly:
alert( Infinity ); // InfinityNaNrepresents a computational error. It is a result of an incorrect or an undefined mathematical operation, for instance:alert( "not a number" / 2 ); // NaN, such division is erroneousNaNis sticky. Any further mathematical operation onNaNreturnsNaN:alert( NaN + 1 ); // NaN alert( 3 * NaN ); // NaN alert( "not a number" / 2 - 1 ); // NaNSo, if there’s a
NaNsomewhere in a mathematical expression, it propagates to the whole result (there’s only one exception to that:NaN ** 0is1).
BigInt
In JavaScript, the “number” type cannot safely represent integer values larger than (2<sup>53</sup>-1) (that’s 9007199254740991), or less than -(2<sup>53</sup>-1) for negatives.
For example, these two numbers (right above the safe range) are the same
console.log(9007199254740991 + 1); // 9007199254740992 console.log(9007199254740991 + 2); // 9007199254740992So to say, all odd integers greater than
(2<sup>53</sup>-1)can’t be stored at all in the “number” type.
String
A string (or a text string) is a series of characters like "Subodh Kumar". Strings are written with quotes. You can use single or double quotes.
For Example
let firstName = 'Subodha';
let lastName = "Mohanty";
We can use quotes inside a string, as long as they don't match the quotes surrounding the string.
let greeting = 'Hi I am "Subodh"';
let message = "Bye";
Boolean
The boolean type has only two values: true and false.
This type is commonly used to store yes/no values: true means “yes, correct”, and false means “no, incorrect”.
For instance:
let nameFieldChecked = true; // yes, name field is checked
let ageFieldChecked = false; // no, age field is not checked
Boolean values also come as a result of comparisons:
let isGreater = 4 > 1;
alert( isGreater ); // true (the comparison result is "yes")
Null
The special null value does not belong to any of the types described above.
It forms a separate type of its own which contains only the null value:
let age = null;
In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.
It’s just a special value which represents “nothing”, “empty” or “value unknown”.
The code above states that age is unknown.
Undefined
The special value undefined also stands apart. It makes a type of its own, just like null.
The meaning of undefined is “value is not assigned”.
If a variable is declared, but not assigned, then its value is undefined:
let age;
alert(age); // shows "undefined"
Symbol
JavaScript added a primitive type in ES6: the symbol. Different from other primitive types, the symbol type does not have a literal form.
To create a symbol, you call the Symbol function as follows:
let s1 = Symbol();Code language: JavaScript (javascript)
The Symbol the function creates a new unique value every time you call it.
console.log(Symbol() == Symbol()); // falseCode language: JavaScript (javascript)
JavaScript non-primitive data types
Object
The object type is special.
All other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities.
The key of a property can be a string. And the value of a property can be any value, e.g., a string, a number, an array, and even a function.
JavaScript provides you with many ways to create an object. The most commonly used one is to use the object literal notation.
The following example creates an empty object using the object literal notation:
let empty = {};
To create an object with properties, you use the key:value within the curly braces. For example, the following creates a new person object:
let person = {
firstName: 'Subodh',
lastName: 'Mohanty'
};
The person the object has two properties firstName and lastName with the corresponding values 'Subodh' and 'Mohanty'.
When an object has multiple properties, you use a comma (,) to separate them like the above example.
Object datatype alone is a very lengthy topic. So we can discuss it in forthcoming articles.
Thanks for reading...



