A Practical Guide to Mastering JavaScript Data | Get the Most Out of Your Code
Namaste🙏🏼
Hey everyone, this is Aryan Sharma's blog, and I hope you're all doing well & great.
I'm thrilled to begin this JavaScript blog for absolute beginners with my Sixth blog post.
📍You can find my blogs here↗️
What to wait? Let's go🚀
Data types
In JavaScript, a value is always a specific type. a number or a string, for example.
In JavaScript, there are eight fundamental data types. We'll discuss them in broad here, and then in more detail in the next blogs of this course.
Unlike in C++, In JavaScript, we don't declare any variable a particular datatype but it is decided after having its value.
let number = "Aryan";
Here we have made the number variable a string.
We can put any type in a variable. For example, a variable can at one moment be a string and then store a number:
// no error
let number = "Aryan";
number = 123456;
In JavaScript, such variables are called “dynamically typed”, meaning that there exist data types, but variables are not bound to any of them.
Number
let n = 123;
n = 12.345;
Both integer and floating point numbers are represented by the number
type.
There are three special values in JavaScript that are considered numbers but don't behave like normal numbers.
The first two are Infinity and -Infinity, which represent the positive and negative infinities. Infinity - 1 is still Infinity, and so on.
Infinity
represents the mathematical Infinity ∞. It is a special value that’s greater than any number.
We can get it when a number is divided by zero:
alert( 1 / 0 );
or just represent to by:-
alert( Infinity );
- A computational error is represented by NaN. It is caused by an improper or undefinable mathematical procedure, such as:
alert( "not a number" / 3 );
BigInt
In JavaScript, the “number” datatype cannot represent large positive and negative integer values.
It ranges from (2<sup>53</sup>-1)
or less than -(2<sup>53</sup>-1)
For most purposes ±(2<sup>53</sup>-1)
the range is quite enough, but sometimes we need the entire range of big integers, e.g. for cryptography or microsecond-precision timestamps.
BigInt
type was recently added to the language to represent integers of arbitrary length.
// the "n" at the end means it's a BigInt
const bigInt = 1234567890123456789012345678901234567890n;
BigInt numbers are rarely used, therefore we won't be covering them here; instead, they get their own chapter. When you require such large quantities, read it.
String
JavaScript requires quotes around any strings.
let str1 = "Hello";
let str2 = 'This is Aryan';
let str = `We can embed ${str1}`;
In JavaScript, there are 3 types of quotes.
Double quotes:
"Hello"
.Single quotes:
'
This is Aryan'
.Backticks:
Hello
.
Double and single quotes are “simple” quotes. There’s practically no difference between them in JavaScript.
Backticks are “extended functionality” quotes.
They allow us to use variables and JavaScript expressions into a string by wrapping them in the template literal `${…}`
let name = "Aryan Sharma";
// used a variable
alert( `Hello, ${name}!` ); // Hello, Aryan Sharma!
Please note that we can only use JS variables in backticks.
alert( "Anything inside double quotes return same {a}" ); // Anything inside double quotes return same {a}
Boolean
The boolean type has only two values: true
and false
.
true
means “yes, correct, 0”, and false
means “no, incorrect, 1”.
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 = 9 > 6;
alert( isGreater ); // as it is true that 9 is greater than 6
// the comparison result is "yes" ( 1 )
The “null” value
The unique null value does not fit into any of the aforementioned kinds.
It forms a separate type of its own which contains only the null
value:
let age = null;
Null is not a "reference to a non-existing object" or a "null pointer" in JavaScript, unlike in certain other languages.
It merely serves as a special value that stands in for "nothing
," "empty
," or "value unknown
."
The code above states that age
is unknown.
The “undefined” value
Undefined, a unique value, also stands out. Like null, it creates a type of its own.
Undefined means "value is not assigned" in this context.
If a variable is declared, but not assigned, then its value is undefined
:
let name;
alert(name); // shows "undefined"
We can also define undefined
to a variable
Objects and Symbols
The type of object
is unique.
Due to the fact that their values can only contain one item (be it a string, number, or anything else), all other kinds are referred to as "primitive". In contrast, collections of data and more complex entities are stored using objects.
The symbol
type is used to create unique identifiers for objects. We have to mention it here for the sake of completeness, but also postpone the details till we know the objects.
The typeof operator
This operator returns the type of the operand.
typeof undefined // "undefined"
typeof 2124355 // "number"
typeof 10n // "bigint"
typeof true // "boolean"
typeof "Aryan" // "string"
typeof Symbol("id") // "symbol"
typeof Math // "object" (1)
typeof null // "object" (2)
typeof alert // "function" (3)
Wohooo🎉 here we have covered DataTypes in JavaScript...
I Hope you loved the blog❣️
See u in the next blog...Stay tuned🎶