JavaScript Struggles — Part 5 | Data Types
Hey there! 👋🏻
We’re now going to explain the data types of JavaScript, I’m only going to talk this time about the data types themselves, ⚠️ I’m not going to explain the (truthy & false) subject now ⚠️.
This’s just a basic view of the data types of JavaScript.
Data types:-
- Undefined
- Null
- Number
- String
- NaN
Undefined
Undefined
is actually a data type in JavaScript.
Even though that is weird, you'll get used to it.
it’s the default data type if you didn’t assign anything in the variable.
var varName;
console.log(typeof varName); // outputs: undefined
Null
Null is a data type that has to be assigned.
The best use of it is to find the undefined
variables.
Also important to know that null is an
object
so whenever you want to check the data type of a variable that contains anull
it'll return "object".
var nullVariable = null;
var nothingHere;
console.log(nullVariable == nothingHere); // that will output true
But the confusing sometimes, but no worries I covered it already in a previous post
Number
The data type number
is so familiar to most developers, it has a number. :)
Simple right?
But sometimes JavaScript has to be JAVASCRIPT and add some weird things around it.
It’s considered bad at math, or to be more specific; it’s actually bad at float-point
Which I’ll cover in an upcoming post, so stay tuned.
String
String
is actually so simple.
It’s just like the string of any other language but with some features.
There are 3 ways of using a string in JavaScript:
- Single quote
'string'
- Double quote
"string"
- And the backticks
Single quote 'string'
The single quotes are used just as the normal string.
let stringVar = 'Hello world!';
Double quote "string"
This is the normal string itself!!
I think it doesn’t need an explanation.
Backticks
What are even backticks?
It’s the string that uses ` instead of “ or ‘
It’s actually the best practice in my opinion for JavaScript as it provides the formatted string feature with it as you use it, and also enables using multiple lines (it doesn’t apply on the web as you need to use <br />
).
let furmattedString = `the answer is ${5 + 5}`;
console.log(furmattedString); // the answer is 10
let multilineString = `Hello there!
I'm Abdelrahman!`;
console.log(multilineString);
/*
Hello there!
I'm Abdelrahman!
*/
NaN
I’ve already explained NaN
in the previous post
So let me be the lazy developer I am and not repeat that again 🥱
That’s all for now! I hope you gain anything new from this post. 😄