Responsive Advertisement

JavaScript Data Types

JavaScript Data Types

JavaScript Data Types

I discussed values and variables in the previous post. We learned before that the values are basically data. Values or data can now have different types in every programming language. Depending on the type of data that we want them to hold. Let's have a look at all of the data types available in JavaScript. To begin with, every value in JavaScript is either an object or a primitive value. When a value isn't an object, it's only a primitive. In this post, we really focus on primitive data types. We can't learn about objects without first knowing about primitive data types. So there are seven primitive data types in the JavaScript programming language. Which is:
  1. Number 
  2. String
  3. Boolean 
  4. Undefined
  5. Null
  6. Symbol
  7. BigInt
So let's look at them one by one.

1. Number: We have a number of data types and numbers are always so-called floating-point numbers, which means that they always have decimals even if we don't see them or don't define them. For example, the 35 value is a data type of number and it is exactly like having a 35.0. However, they are both the number data type. In many other programming languages, you will find different data types for integers and for decimals, but not in JavaScript. All numbers are simply types of numbers.

2. String: A string is simply a series of characters. The string data types are just used for text, and you should always put them in quotes, no matter if they are double quotes or single quotes. Otherwise, JavaScript will actually confuse you with string data and variable names. So, if you want to string, you must write within the quotation marks; otherwise, JavaScript treats the text as a variable which is really stupid. So, mind it very carefully.

3. Boolean: The Boolean data type is a logical data type. That can only take one of the logical values, true or false. To put it another way, a Boolean is always true or false.

These are the three most important data types that we will deal with the most but there are still four more which might be a bit more confusing.

4. Undefined: Undefined is the value taken by a variable that is not yet defined. The variable that is not yet defined is simply just a variable that we declared but without assigning a value. For example, like the below image example or see the code snippet variable which is declared inline no. 4, So basically, we can say that undefined means an empty value. 

JavaScript Data Types Example

5. Null: null, which is actually pretty similar to undefined data because it also means empty value, but it's used in different circumstances. But null data types are objects. You can learn more about this in Object-Oriented JavaScript, but you can get a sense of it here. JavaScript says that a type of null is an object. This is regarded as a bug or an error in JavaScript. However, this bug was never corrected for legacy reasons, but now it's of course not an object. This should return null just as the type of undefined returns undefined.

let number = 10;
let string = "some text";
let boolean = true;
let undefiend;
let nullData = null;
console.log(typeof(number));  // output of the number
console.log(typeof(string));  // output of the string
console.log(typeof(boolean));  // output of the boolean
console.log(typeof(undefiend));  // output of the undefiend
console.log(typeof(nullData));  // output of the nullData

let countryName = "Bangladesh";
console.log(typeof(countryName));
countryName = 100;
console.log(typeof(countryName));
countryName = true;
console.log(typeof(countryName));

// This is single line of comment

/*
This is
multiple line
of comment
*/

6. Symbol: JavaScript has symbol data types that were introduced in ES2015. But this data type is not really useful for us. It simply defines a value that is unique and cannot be changed, like a constant value.

7. BigInt: Finally, starting in ES2020, there is also a BigInt, which is for integers that are too large to be represented by the number type. So, basically, it's another tire before numbers, which we'll discuss in another post.

So these are the seven primitive data types in JavaScript. But there is another fundamental thing to know about types, which is the fact that JavaScript has a feature called dynamic data typing. This means that when you create a new variable, you won't have to specify the data type of the value it contains manually. You must do this in many other programming languages, but not in JavaScript. Instead, JavaScript automatically determines the data type of a value when it's stored in a variable. In JavaScript, the difference between value and variable is important. It is the value, not the variable, that has a type. As a result, variables just store values with a data type. So variables simply store values that have a type. Many people don't know about this detail or just don't care, but this is how it actually works.

We can easily give a new value to the same variable that is of a different data type. For example, variable A can initially be a number and then later be a string. That's not a problem at all. This can, of course, be very useful, but it can also be the source of some difficult-to-find bugs, which means errors in our code. All right, so let's not actually see these things in action. But first, let's discuss code commenting before we go into data types. So we use comments in programming to come in and code or to deactivate code without deleting it. We can define a comment in two ways, which are a single line and a multiple lines of comments. For example, see line 19 of the code snippet which I provided above. I could write a single-line comment with // and then describe what this code does or whatever you want. Alternatively, multiple lines of comments, such as line numbers 21 to 25 in the code snippet, start with /* and ends with /*. That's it. So anything that comes after // or into /* */ JavaScript will completely ignore anything. This can be really useful to kind of divide the You can comment beside the variable for easy understanding after you or other people.

Now let's get back to the topic of this article, which is data types. We know that JavaScript programs are executed from the top to the bottom line of code. In line 12, we have a variable called countryName that stores a string value. which is displayed in the browser console with line number 13. In line 14, I am re-assing the countryName variable with 100. There are a number of data types. Remember that when you re-assign any variable, you should only write the variable name without the let keyword. It means when we want to change the value of the variable, we simply write it again, like inline number 14 without the let. So we simply assign a new value here to this already existing variable. We first declared a new variable with the countryName variable and assigned it this "Bangladesh" value, which is a string. And then, down line number 14, we changed the value data in the variable. For example, we have a box and initially put a book in a box, but later take the book out of the box and instead put a phone in a box. We simply changed the content of the box. In our example here, we changed from a string to a number. After a few lines, we reassign the countryName variable to true, which is a boolean. We get the result in the browser console in line 17 output. We are simply reasserting the variable value, but JavaScript automatically defines the data types. This whole thing or concept is called dynamic typing. 

After that, if you have any queries or confutation about JavaScript primitive data types, comment below.

Post a Comment

0 Comments