DEV Community

Cover image for JavaScript Struggles - Part 5 | Data Types

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 itself, ⚠️ 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

Undefined is actually a data type in JavaScript.
Even though that is weird, but 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
Enter fullscreen mode Exit fullscreen mode

Null

Null is a data type that have 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 a null it'll return "object".

var nullVariable = null;
var nothingHere;

console.log(nullVariable == nothingHere); // that will output true
Enter fullscreen mode Exit fullscreen mode

But the confusing sometimes, but no worries I covered it already on a previous post


Number

The data type number is so familiar for most developers, it have 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 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's 3 ways of using string in JavaScript:

Single quote 'string'

The single quotes is used just as the normal string.

let stringVar = 'Hello world!';
Enter fullscreen mode Exit fullscreen mode

Double quote "string"

This is the normal string itself!!
I think it doesn't need an explanation.

Backticks

What is even backticks?
It's the string that uses ` instead of " or '

It's actually the best practice in my opinion for JavaScript at it provides the furmated string feature with it as you use it, also enables using multiple lines (it doesn't apply on 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!
*/

Enter fullscreen mode Exit fullscreen mode

NaN

I've already explained NaN in 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. 😄

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

A number of problems here:

  • NaN is not a data type (its type is actually Number)
  • You've omitted the following types:
    • Symbol
    • BigInt
    • Boolean
  • You haven't mentioned the Object type, although maybe you were just sticking to the primitive types.
Collapse
 
abdelrahman_dwedar profile image
‘Abdelraḥman Dwedar 👨🏻‍💻🇵🇸

Thanks for noticing me for that issues I'll fix them.

I totally forgot to add the boolean & symbol.
And I'll add the BigInt later as it needed more time to write about (and research).

But yes I were sticking to the primitive types in this article, as I'll add object in another one with the data structures.

Thanks for reminding me.