You are currently viewing What is Primitive Datatypes in Javascript with Examples

What is Primitive Datatypes in Javascript with Examples

Primitive Data Types in Javascript with Examples

In this article about Javascript primitive data types with examples, data types indicate the type of data. Javascript data types can be divided into three types, they are :

  • Primitive data types
  • Special data types
  • Composite data types

Number type, string type, and boolean type are considered primitive data types. Undefined types and null types are considered Special datatypes. Object types are considered composite types. Let’s discuss primitive datatypes.

What is a primitive datatype?

It is also called value types, we can create two different types of variables one value type and another one is a reference type. Value type variables hold the value they hold a value or a given value directly whereas reference type variables hold a reference to a pointer or address of another memory location. They are divide into three:

  • Numbers
  • String
  • Boolean

1. Numbers

It Indicates both integer and real numbers.

Integers:

It means whole numbers, numbers that don’t contain a floating point or a decimal point. All below are examples of integers, which means negative infinity to positive infinity any number which has no decimal point is considere an integer.
Ex: 12, 2000, -400, etc

Real numbers:

Numbers containing floating points are real numbers or float numbers. In the given below all numbers are examples of real numbers. we can use both variable and constant for initializing integer

Ex: 3.25, 9,0, -15,3 etc

Example code:

var playerscore=10 ;
const PI= 3.142;
document.write("Player Score = ", playerscore,"<br/>","PI = ", PI)) ;

Output:
Player Score = 10
PI = 3.142

2. String

It indicates the sequence of characters enclosed in double quotations or single quotations. i.e. Textual data. So, the string is one or more characters enclosed in quotations.

READ ALSO  3 Types of Age Calculation in JavaScript

Note:
If a string begins with single quote then it has to end with single quote and If a string begins with double quote then it has to end with double quote, you can don’t mismatch.

Ex:
“Hello World”
‘Hello JavaScript’

Example code:

var player="Johny";
document.write("Player Name = ", player);

Output:
Player Name = Johny

3. Boolean

It indicates a logical or conditional result. The boolean data type has only two values they are “true” and “false” values. In JavaScript, the true boolean value indicates 1 and the false boolean value indicates 0.

Ex:

var gameover = false;
document.write("Type of Is Game Over = ",typeof gameover);

Output:
Type of Is Game Over = boolean

In the above code, we assign a “false” value to the variable gameover. We check the data type using “typeof” keyword it returns a boolean.

Leave a Reply