JavaScript Primitive Types

 

JavaScript Primitive Types

What is the kind of values that we can assign to a variable? Well, we have seen strings, but we have more types.

Basically, in JavaScript we have two categories of type.

1.Primitive types or value types.

·         String

·         Number

·         Boolean

·         Undefined

·         Null

2.Reference type.

·         Object

·         Array

·         Functions.

 

Let name = ‘mosh’; Called String literal

Let age = 30; Called Number literal

Let approved = true; Called Boolean literal

Or

Let approve = false; Called Boolean literal

We use Boolean literal when we use Some logic.

For example: if the order is approved, then it needs to be shift, So the value of Boolean literal can be true or false. By the way, it can be either true or false. It cannot be both true and false, by the way, both true and false are reserved keywords, so they cannot be. Variable names.

let firstName ; Nothing mention for value so Undefined is also this type of value.

let firstName = undefined; called undefined.

let firstName = null; also called undefined.

We use null in a situation where explicitly to clear the value of the variable.

For example, we may want to present the list of users with the List of Colours, If the user not selected any colour. Then you want to set selected colour variable to null.

let selectedColor = null;

If the user reassigned the variables to colour like Red. let selectedColor = Red;

So, we use null in situation where we want to clear the value of the variable.

 

JavaScript is Dynamic Typing,

One thing that separate JavaScript from lot of programming language is that JavaScript is dynamic language.

What is mean by dynamic? We have two types of programming language,

1.Static (Statically typed) string name =’Emil’;

In static language when we declare the variable, the type of the variable is set and it cannot be changed in future.

In a dynamic language like JavaScript

2.Dynamic (Dynamically typed) let name = ‘Emil’;

In dynamic language like JavaScript variable can change.

For example:

let name = ‘Emil’;


Now go to console and type


typeof name.


To check its type


Now you will get Answer= string.

Now reassign the value for the type in console

name = 1;

Now in console. You will get the answer = 1


Now type? Typeof in console now check its type name.

Answer = number


Look now the type change two number. This is what we call dynamic language. By the way, typeof is another reserved keyword. We cannot have the variable called typeof.


let selectedColor=null;

In console Type

typeof selectedColor

Answer= Object.

The type of this variable is object.

Post a Comment

Share your thoughts here

Previous Post Next Post