Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Primitive Data Types in JavaScript

Introduction to Primitive Data Types in JavaScript

Primitive Data Types in Javascript are majorly divided into two i.e. Primitive data types and user-defined datatypes. Primitive data types are the built-in data structures defined in the system for the language. User-defined data types are data structures which are defined by the user/programmer itself. There are two types of languages. Strongly typed languages and loosely typed languages. These are often termed as static and dynamic languages. Strongly typed languages make it compulsory for the user to specify the datatype of the variable being declared while loosely coupled languages allow declaring variables without specifying its data type. The variables in loosely coupled languages are not associated with any particular data type and can be further converted to other types depending on manipulations. Javascript is a loosely typed language.

Primitive Data Types in JavaScript

Ecma International that is ECMAScript has specified 8 types of possible datatypes in javascript in its latest version. Out of which seven are primitive and one is non-primitive. Primitive data types are all immutable which means whenever the value is assigned to such data typed variables their value cannot be changed and if done so, will result in new memory allocation being done for the same. Following is the list of primitive data types in javascript.

  • Boolean
  • Null
  • Undefined
  • Number
  • BigInt
  • String
  • Symbol

The remaining non-primitive data type is an object. We will discuss all the primitive data types one by one in the below section of the article.

1. Boolean

 It is considered as the logical entity which can have either true or false value. 0, -0, null, false, NaN, undefined, or an empty string (“”) are all considered as false value by the boolean object. All other values other than above will be treated as true valued boolean which also includes blank arrays and string “false”. Let’s understand its working with the help of an example.

Code:




Boolean Primitive value Demonstration


Boolean Primitive value Demonstration




Output:

2. Null

This datatype has only one possible null value. A null value means that an object is absent for that variable/object where it is assigned.

3. Undefined

It is a non-writable, non-enumerable and non-configurable primitive vale in javascript which represents that the object is not assigned any value yet. The default value of any object is undefined. Let’s see an example of it.

Code:




Undefined Primitive value Demonstration


Undefined Primitive value Demonstration




Output:

4. Number

It is the built-in numeric type of javascript which is 64-bit binary format IEEE 754 value which is double-precision whos range is -(253 − 1) and (253 − 1). The number data type has also three symbolic values namely +Infinity, -Infinity, and NaN that stands for Not a Number. Number.MAX_VALUE and Number.MIN_VALUE can be used to get the largest and smallest value of +-Infinity. There is only one integer in Number format which has two representations which are 0. Its two representations are +0 and -0.0 is the alias of +0. They both resemble the same in practice but when dealt with dividing the value they behave as two different entities and result accordingly. Let us study their behavior with the help of an example.

Code:




Number Primitive value Demonstration


Number Primitive value Demonstration




Output:

5. BigInt

The Number data type has some safe integer limit. However, by using BigInt, we can represent integers with arbitrary precision and beyond the safe integer limit. We can use BigInt by either passing it as a parameter to BigInt or appending n at the end of a numeric value. Let us see an example of the same.

Code:




BigInt Primitive value Demonstration


BigInt Primitive value Demonstration




Output:

6. String

Javascript strings are immutable and this is used for textual representations. It is a set of unsigned 16-bit integers holding each element. Each element has its position. The first one being placed at 0 indexes, second on 1 index and so on. The total number of elements is the length of that string. Even though it is immutable, javascript has provided a huge set of string methods that can be used to manipulate the original strings and retrieve the modified one. For example, if we want to obtain substring of a string, we can use String.substr() method to do so. For concatenation, we can use String.concat() method.

7. Symbol

Symbol datatype is newly introduced in ECMAScript 2015. It can be used as the object’s key. It is an immutable and unique value. Symbols are called atoms in some programming languages.

Recommended Articles

This is a guide to Primitive Data Types in JavaScript. Here we discuss the brief overview of the top 7 Primitive Data Types in JavaScript along with code implementation and output. You can also go through our other suggested articles to learn more –

  1. JavaScript String Format With Methods
  2. Introduction to Javascript Nested Functions
  3. JavaScript Date Function (Example)
  4. Functional Programming in JavaScript

The post Primitive Data Types in JavaScript appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Primitive Data Types in JavaScript

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×