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

Data Types in Javascript | Primitive, Examples

A data type in Javascript specifies the specific type of value that a variable can store. Internally, a variable represents a memory location where data is stored.

Data is information that we store in computer programs. For example, name, age, the color of your hair, how many siblings you have, where you live, whether you’re male or female, numbers, etc. All these things are data.

JavaScript is a dynamic and loosely typed language. It means that we do not need to specify the type of a variable because it is dynamically used by the Javascript engine.

Javascript simply allows creating a generic variable using the keyword var. The actual data type used depends on the initial value that we will assign to the variable.

Consider the following examples:

var FirstName = “Shubh” // Holding string.
var AccountNumber = 123499 // Holding number.

As you observe in the above code, the variable var can hold any type of values such as strings, numbers, etc. Hence, Javascript is considered as a “loosely typed language“. We can put any data to a generic variable.

Other languages (Java, C, C++, etc.) will not allow to create a generic variable of an unspecified type. These languages are known as “strongly typed languages”.

Types of Data types in Javascript


Like any other programming language, JavaScript language allows us to work with the seven data types. A list of all data types are as follows:

  1. Numbers
  2. Strings
  3. Booleans
  4. Null
  5. Undefined
  6. Objects
  7. Arrays

The first three data types are called primitive data types in Javascript. The next two are called trivial data types and the last two composite or reference data types.

Let’s understand the first three primitive data types with various example programs.

Javascript Primitive Data Types


Primitive data types are the set of all basic building blocks for which data can be represented. They are manipulated by value. In Javascript, there are three types of primitive data types.

  • Numbers
  • Strings
  • Boolean

Let’s understand all three primitive data types one by one.


1. Numbers:

Numbers in Javascript are used to represent both integers and floating-point numbers. For example, your age can be represented as a number. Numbers look like this: 1, 23, 10.50, 23.8990, etc.

Like other languages like C, C++, Java, Javascript language does not provide special or separate data types for integers and floating-point numbers.

It does not make a difference between integer values and floating-point values. It considers all numbers as floating-point values.

JavaScript language represents numbers by using the 64-bit floating-point format defined by the IEEE 754 standard.

Let’s take a very simple example program where we will assign some numeric values to variables.

Program code 1:


Output:
           5
           10
           15
           20.25

2. Strings:

A string is another type of data that can be saved to a variable. It is basically a line of text surrounded by quotation marks.

Enclosing a line of text in a single or double quote indicates that it is a string. For example:

var name = "Shubh"; // String.
alert( name ); // This will alert "Shubh".

When wrapping a number in quotes and assigning it to a variable, Javascript will consider the number as a string. For example:

var num = "5";
alert( num + num ); // This will alert "55".

We know that plus sign (+) is used to add numbers. When plus sign (+) is used with strings, it concatenates strings together like this:

var firstName = "Shubh";
var lastName = "Deep"
document.write(firstName + lastName); // It will print ShubhDeep.

If we add a number and string, Javascript will assume the number as a string. For example:

1. var x = "5";
    var num = "five";
    document.write(x + num); // It will print 5five.

2. var num = 25 + 30 + "five";
    document.write(num); // It will print 55five because Javascript will treat 25 and 30 as numbers. That’s why it is adding.

3. var num = "five" + 20;
    document.write(num); // Print five20.

4. var num = "five" + 20 + 30;
    document.write(num); // Print five2030 because the first operand is a string, all operands are considered strings.

3. Boolean:

We can also assign a variable with boolean values (true or false). Javascript provides built keywords true and false to define boolean values, so quotation marks are not necessary.

For example:

var booleanValue = true; // The variable booleanValue is now true.

Booleans are often used in conditional testing. For example, consider the following statements:

If (num > 18) {
      // do something
}

A Boolean expression (num > 18) is used within the if statement to check whether the code within the braces will be executed.

If the variable num is greater than 18, the Boolean expression evaluates to true; otherwise, it evaluates to false.

Trivial Data Types


1. Null:

Null is another special data type in JavaScript. Null means nothing. It represents and evaluates to false. Assigning a value null to a variable means it contains nothing. For example:

var value = null;
alert(value); // This will open a dialog containing "null".

Don’t confuse between null and empty value. Empty value is different from null, which is just plain nothing. For example, declaring a variable and assigning its value to an empty string looks as:

var myVariable = ' ';

The variable myVariable is empty, but not null.


2. Undefined:

Undefined is the declaration of a variable without a value. The type is also undefined. For example:

var num; // Value is undefined, type is undefined
document.write(num); // It will print undefined.

Here, there are two lines of code. The first line declares a variable named num. The declaration of variable tells the javascript engine to allot a memory space that we can use to store data. As we are not assigning any data to variable num, the subsequent line will return undefined.

Undefined is different from null, but both null and undefined evaluate the same way.

Javascript Non-primitive Data Types (Reference Data Types)


There are three non-primitive data types in javascript. Non-primitive data types are also called composite or reference data types in javascript. They are as follows:

  • Objects
  • Arrays
  • Functions

These non-primitive data types are manipulated by reference.

Let’s understand all three non-primitive data types in javascript with example programs one by one.


1. Objects:

Objects in JavaScript are a set of properties, each of which can contain a value. These properties are written as name: value pairs (or key: value pairs), separated by commas.

Each value stored in the properties can have a value, another object, or even a function. We can also define our own objects or can use several built-in objects.

Javascript objects are created with curly braces. Let’s understand them with help of some examples.

a) var myObject = { }; // an empty object.

b) An object with several properties:
    var person = {
                              firstName: "Ivaan",
                              lastName : "Sagar",
                              age: 04,
                              eyeColor: "blue",
                              hairColor: "black"
                            };

The preceding code example creates an object called person, that has five properties: firstName, lastName, age, eyeColor, and hairColor.

The values contained in each property are Ivaan, Sagar, 04, blue, and black respectively.

The .operator is used to access the property of an object. For example, we can access the lastName property of the person object like this:

document.write(person.lastName); // It will print Sagar.

The complete program code structure is as follows:


   
Output:
           Full name: Ivaan Sagar
           Age: 4
           Eye color: blue
           Hair color: black

We will learn more about objects later in the further tutorials.


2. Arrays:

An array is a group of multiple values that can be assigned to a single variable. The values in an array are said to be indexed.

The first member is given the index number 0, the second is 1, the third is 2, and so on. Javascript array is written with square brackets ([ ]) and items in an array are separated by commas.

Let’s understand it with the help of an example program.


  
Output:
           20
           40
           50

We will learn more about arrays later in the further tutorial.


3. Functions:

A function is a block of Javascript code that is written once and can be executed when “someone” calls it.

Javascript provides some predefined functions and we can also define our own function called user-defined functions. The definition of a function looks like this:

function sum(x, y) {
     return x + y;
}

We will learn more about functions data type in the function chapter of javascript.

Typeof Operator in Javascript


The typeof operator in Javascript is used to find the type of a JavaScript variable. It returns the type of a variable or an expression.

Let’s understand it with the help of an example program.

  
    
Output:
          number
          string

Hope that this tutorial has covered almost all the important points related to Javascript data types with various examples and programs. I hope that you will have understood the basic points of primitive and non-primitive data types in Javascript.
Thanks for reading!!!

The post Data Types in Javascript | Primitive, Examples appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Data Types in Javascript | Primitive, Examples

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×