/  Technology   /  Data Types in JavaScript
data types

Data Types in JavaScript

 

A DataType describes the different kinds of data that we can work with and storing in variables in javascript.

JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript that is Primitive and Non- Primitive

The Primitive Type includes

  • String
  • Number
  • Boolean
  • Null and
  • Undefined

Where as the Non Primitive Type includes

  • Object
  • Arrays

 

Primitive Types :

String Data Type

The String Data Type includes all textual data. Which should be provided in quotes.

var message = “hello”;

Number Data Type

A integer or a floating point can be declared as Number Data Type in JavaScript.

var number = 42;

Boolean Data Type

Boolean Type represents a logical entity and can have two values that is True and False.

let nameFieldChecked = true;
let ageFieldChecked =false;

Boolean value can also be used to make comparisons

let isGreater = 5 > 1;
alert(isGreater);   // True will be returned.

Null

The Special null value does not belong to any of the data Types described above.

It has its own separate type which contains only the null value.

Null represents no value at all.

let age = null;

Here null represents “empty” or nothing or value unknown.

Null is treated as falsy for boolean operations.

Undefined

The Special value Undefined also stands apart and different than previous data types.

The meaning of undefined is “value is not assigned”.

If the variable is declared, but not assigned, then its value is undefined.

let age;
console.log(age);  // displays undefined.

 

Non- Primitive Types

Object

The ‘object’ is a non-primitive data type in JavaScript. Arrays and Functions in JavaScript belong to the ‘object’ data type.

An Object is a set of key and value pairs which is enclosed with flower brackets.like:

Var Obj = { a : 5, b : 6};

When we refer to Obj, we are actually referring to the address in memory which contains the value {a: 5, b: 6}, instead of the value {a: 5, b: 6} directly.

We can change the values of Obj.

Obj1[a] = 7 ;
console.log(Obj);    // will return the value { a : 7, b : 6}

We can even check the data type :

typeof(Obj);   //will return “object”.

Arrays

An array in JavaScript is an object data type. An array contains more than one value with a numerical index, where the index starts from 0. Thus it holds its value in a key-value pair. Array is a list of elements enclosed in square brackets. The elements of array can be accessed with indexing.

var arr1= [7, 2, 4];

We cannot change the above arr1. For example we try to change its value.

Arr1[0] =  4
console.log(arr1) // This will return the array [ 4, 2 , 4].
typeof(arr1)  // will return the data type “object”.

The array arr1 refers to the address in memory which contains the value [ 4 , 2, 4].

 

Leave a comment