Primitive Types in JavaScript


In this article, we will talk about Primitive Types in JavaScript. In this tutorial, we will discuss data types in JavaScript. There are two types of data in JavaScript. Primitive type and object type. so in this lecture, we will discuss primitive types. Keep in mind JavaScript Data or you can say values are at the primitive or object. Primitive data types are immutable. Immutable means you can’t change the value of the variable after you initialize it.

 Let’s take an example,

We specify 24 value to the age variable. The number 24 will always be the number 24. You can’t change it. the same way string also work. If you specify a string for a particular variable then you can’t change the string because of it is immutable.

If you specify 32 the age variable then you will get 32 on the console. The variable value is modifying. It is not. When you specify a new value to the variable. It will not modify the previous value but instead of this, it will create a new variable.

The same way work for the string type also. But what if you concatenate two string using Plus ( + ) sign.

Using + sin you can concatenate two string. But keep in mind as I said the string is also immutable. So the concatenation represents a new variable. When you concatenate two string it will create a new variable with the concatenated string. not the modified string.

JavaScript recognizes four type of numeric literals

  • Binary
  • Decimal
  • Octal
  • Hexadecimal

Using decimal literal you can specify number value decimal value with 10 exponential Notation. Then you can specify hexadecimal octal and a special value Which is infinity and negative infinity. We also have a special word, not a number to specify this is not a number. But infinite and not a number is not a numeric literal. It isn’t but These values are taken as a placeholder.

String

We learned how to declare a string literal. A string is just a simple text data. We can specify the string in the single or double quotes. Or you can use both. We will not dive Deep into string topic because it is beyond the article. So we will clear all the concept and all the small detail information of string in the separate video. So I had just cleared the basic concept of string here.

 

Boolean

What is Boolean?  Boolean is just a value type That has only two possible values. True or False. But be careful when you’re using a Boolean type because if you intended to use the boolean then don’t specify the Boolean type in the Quotation mark. If you specify the Boolean value in the quotation mark then the literal Equal to the string literal.

The best way to create a Boolean type is this.

 

Symbols

In ECMAScript 6 we have a new type symbol. Symbols are the new data type in ES6 which represent Unique tokens. Once you create a symbol it is unique and it will not match any other symbols. in the future lectures, we will take a brief look at symbols. But just for the basic I just wanted to show you how to create a symbol.

Symbols are created using the symbol constructor. We will learn what is constructor functions and all that in the future tutorials.

Now if you check this constant then you will get false. Because as I said symbols are Unique. Every symbol has the unique token. So if you print this statement you will get false.

Null and Undefined

JavaScript has two special types null and undefined. Now has only one possible value which is Null and undefined has only one possible value undefined. Both null and undefined Represent something that doesn’t exist.

It means you haven’t specified any value to the identifier. But most programmers use null type to check the value is exist or not. You can also use undefined also but giving undefined value to the variable is sometimes complicated because If you specify a variable without explicitly specifying a value It will have a value undefined by default.

 so let’s take an example.

If you just specify identifier without any value then you will get undefined. If you specify no value to the identifier then you will get now type. If you specify a numerical value to the identifier then you will get that numerical value And the last if you specify undefined then you will get the same result undefined.

That’s it. You are primitive data type in JavaScript is explained. If you have any questions regarding this topic write down in the comment. read more subsequent articles and also the previous articles for the more latest update.