Learning Javascript – Knowing the Data Types

Like all other programming languages javascript also have the types which we should be aware of while programming.In this article I will discuss all the data types in JavaScript like Boolean types, string types, arrays, reference types and functions with examples which will make it easier to understand.

JavaScript has a number of primitive types which are described below

Data Types in Javascript

  1. Value Types
    1. boolean
    2. string
    3. number
  2. Reference Types
    1. object
  3. Delegate types
    1. function
  4. Special Types
    1. undefined
    2. null

At any point while programming we can get to know about the type of the variable using the typeof operator

Special Types in JavaScript

Along with other primitive types we have special types in Java Script which are “null” and “undefined”.

Null is as same as the null reference of C#. null means that the variable has not been instantiated and is not initialized in memory.But undefined is something different. Undefined means that the variable has never been defined before using it.

Below is the code snippet for both of the examples.

Not A Number (NaN)

There are cases where one of the operand is number and the other operand is not a number. We can get the type for the output for this operation using isNaN as shown in the below code snippet.

As shown in the above code isNaN is a function which is used to find if the variable provided is a number or not.

Boolean Types in JavaScript

Boolean types in JavaScript are only the true and false and not any number. But there are some cases which are treated as true or false and are shown in the below code snippet.

As shown in the above code snippet there are many ways in which we can determine true and false like an empty string returns false where non-empty string is true, number greater then 0 is true and 0 is always false. Nan is false and many more cases.

String types in Javascript

Just like C#, string types are immutable in JavaScript. But we can enclose strings in JavaScript in single quotes as well as double quotes as shown in the below code snippet.

Unlike C# these two forms of representing string is same where in C# single quotes represent characters. This notion of representing strings in javascript helps us to embed single quotes when string is enclosed in double quotes and vice versa.

Like C# we can use array accessor to get the individual element within the string. In the below code snippet I get the second element of the string as this is also 0 based indexer.

Further I can use the length property of the string to get the length of the string. But here we should be aware of one thing that length usually represent the length of bytes in a string not just the actual characters.

Reference Types in JavaScript

Like C# we have reference types in javascript. In C# all the reference types are derived from the object class. In javascript all the objects that we will be dealing with are typeof object.

The two objects which we are going to deal with in javascript are object and arrays. Note that the data type for both of these objects is going to return typeof object.

Arrays can hold a collection of value type as well as reference type.

We can create objects in javascript as shown in the below code.

Here I have created an object person which contains two properties named firstname and “last name”. Objects in javascript contains the properties in form of name and value pair. One more thing to notice here is that we can have spaces or any other reserved characters but these should be enclosed in double quotes.

We can also have objects contained by other object as shown in the below code. Here I have person object which is enclosing one more object named address apart from other name value pairs

Creating an array in javascript

An array can be created in javascript as shown in the following code

Arrays in javascript are untyped collection unlike C# where we can work with strongly types collections. In the below code snippet I have displayed some of the important functions of array which we can use while working with them.

Functions in JavaScript

Functions in Javascript are again type of data types in javascript. We can think of functions as Func<> type of C# where we assign come operation to a variable to performed later on.

Below is the code snippet for a function foo in javascript with two arguments

Functions can also be the part of the object as shown in the below code snippet.


In this article I have discussed about the basic building blocks of javascript which can help us while developing application.

Vikram Chaudhary

I have 8 years of experience in developing .NET applications. I have worked on both web as well as windows based application using ASP.NET and WPF. I have keen interest in learning and implementing new things. You can always get in touch with me at vikram.chaudhary@dotnetforall.com or vikram.vee@outlook.com. Please let me know your thoughts about this article