X



Home>Tutorial>

What is Javascript Data Types and Types of JavaScript Data Types

Nuelson Penuel Sunday, August 22, 2021 Javascript

Every programming language deals with data types at such, JavaScript is not an exception. JavaScript has different data types to hold different types of data or information. In JavaScript, we have two types of data types which are primitive and non-primitive However, JavaScript is known to be dynamic type language meaning when declaring a variable you need not to specify type of the variable because as it will be used dynamically by JavaScript engine. Using the keyword var to declare a variable can make it hold any value be it strings or numbers. In this post, you will learn about JavaScript data types and type of JavaScript data types, primitive and non-primitive data type.

var mystr;

The variable mystr in the above example can be used to hold any type of value such as date, number, double, string e.t.c.

What is JavaScript primitive data type

Is a kind of data type specifies the size and type of variable values, and it has no additional methods.

Types of JavaScript Primitive data types

In JavaScript, there are five types of primitive data types. They are as follows:
  1. JavaScript String Data Type
  2. The string data type is used to represent sequences of characters. Strings can be created using single or double quotes surrounding one or more characters e.g.
    var mystr = "Am giving you $200"; 
    
    var mystr2 ='I love this';
    
  3. JavaScript Number Data Type
  4. Number is a data type that represents numeric values.These numbers could be positive, negative, exponential values or contain decimal point.
    
    var mynum1= "2";
    var mynum2 = "3.5";
    var mynum3 = "-3";
    var mynum4 = "3.55+e7";
    
  5. JavaScript Boolean Data Type
  6. The Boolean data type is used to hold only two values. In order words, it returns true of false e.g yes (true) or no (false), on (true) or off (false) and true or false. A typical example is given below;
    var amDone = true;   // yes, am done with what am doing
    var notDone = false; // no, am not done yet
    
    Boolean can also be used to compare two values in a program. The example give below will be used to compare two variables and display the result using alert box.
    var n1 = 5;
    var n2 = 6;
    alert (n1 > n2);//result is false
    alert (n1 < n2);//result is tue
    
  7. JavaScript Undefined Data Type
  8. In JavaScript, the undefined data type can only return one value known as special value undefined. The Undefined value is returned only if a variable is declared but has no value assigned. If you try to return such variable value, you will see the undefined result.
    var x = "I have a value";
    var y;
     alert(x) // Output: I have a value
    alert(y) // Output: undefined
    
    Simply put, undefined data type signifies undefined value
  9. JavaScript Null Data Type
This is a data type that can have only one value at a time i.e the null value. A null value represents no value. Null does not refer to empty string ("") or 0, it is simply nothing. When you assign the null to a variable, it will override any existing value hold by that variable. An example of how null can be used is given below;
var x = null;
alert(x); // result is null
var y = "Welcome to Emmason tutorial";
alert(y); // result is welcome to Emmason tutorial
y = null;
alert(y); // result is null

What is JavaScript non-primitive data type

The non-primitive data types are as follows:
  1. JavaScript Object Data Type
  2. The object is a complex data type that can be used for the storage of collections of data. It can be used to create a variable that can hold several information. For instance, an object of Personal Information. An object usually contains properties that are defined as a key-value pair. A property name or key is always a string, but the value can be any data type, like numbers, booleans, string or complex data types like arrays, function and other objects. Below is an example of working with object.
    <script>
    var mycart = {
    
    "Product": "Ice Cream", 
    "Make": "Vanilla", 
    "Color": "Tea",
    "orderID":"0983"
    };
    
    //For you to access the object property values. Try this
    alert(mycart.Product);
    </script>
    
  3. JavaScript Array Data Type
  4. Array represents group of similar values. Furthermore, array is a type of object that can store multiple values in single variable. In an array, all its elements has a numeric position, known as its index, and it may contain data of any data type such as: strings, booleans, functions, objects, and even other arrays. Array index stars from 0. The simplest way to create an array is by separating the array elements by comma enclosed by square brackets, as shown in the example below:
    var names = ["John", "Peter", "Emmason", "Penuel"];
    alert(names[0]);   // Output: John
    
    
    Learn more about array at: more about array and how to create
  5. RegExp
  6. RegExp represents regular expression
  7. JavaScript Function Data Type
  8. A function is an object that consists of block of codes that are executed upon request. Since functions are objects, so it is possible to assign them to variables, as shown in the example below:
    var add = function(){ 
    var a = 3;var b = 4;
        return sum = parseInt(a) + parseInt(b); 
    document.log(sum);
    }
     
    A function can be can be stored in variables, objects, and arrays. Functions can be passed as arguments to other functions, and functions can be returned from functions. Read more on function at how to write a function

| | Views(329)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services