Premium Courses

Learn practical web development building multi user SEO blog app in Laravel. A Journey from localhost to live server.

Learn the fastest growing and high in demand React Js in short period of time along with ES6. Build a Newsapp while learning.

A complete beginners guide to Web Development. Master the art of building Responsive web pages with this course. Learn to code from scratch!


JavaScript Data Types - Getting Started with JavaScript Data...

JavaScript Data Types - Getting Started with JavaScript Data Types

Category: JavaScript   Updated by Ryan Dhungel



The basic data types in JavaScript are Undefined, Null, Boolean, Number, and String. There is also one complex data type called Object, which is an unordered list of name-value pairs.

The typeof operator

You can use typeof operator to determine the data type of a given variable. Using the typeof operator on a value returns one of the following strings:

  • undefined if the value is undefined
  • boolean if the value is a Boolean
  • string if the value is a string
  • number if the value is a number
  • object if the value is an object (other than a function) or null
  • function if the value is a function

Some examples of using typeof operator

var message = “some string”;
alert(typeof message); //”string”
alert(typeof(message)); //”string”
alert(typeof 95); //”number”

The Undefined Type

The Undefined type has only one value, which is the special value undefined. When a variable is declared using var but not initialized, it is assigned the value of undefined.

// variable message is declared without initializing
var message;
alert(message == undefined); //true
// variable message is explicitly initialized to be undefined
var message = undefi ned;
alert(message == undefined); //true


The null Type

The Null type is the second data type that has only one value called null. It is an empty object pointer.

var car = null;
alert(typeof car); //”object”
if (car != null){
  //do something with car
}

The Boolean Type

The Boolean type has only two literal values true and false. These values are distinct from numeric values, so true is not equal to 1,

and false is not equal to 0.

The Number Type

The Number type represent both integers and floating-point values.

var intNum = 55; //integer

Floating-Point Values

To define a floating-point value, you must include a decimal point and at least one number after the decimal point.

var floatNum1 = 1.1;
var floatNum2 = 0.1;
var floatNum3 = .1; //not recommended

Range of Values

Not all numbers in the world can be represented in JavaScript, because of memory constraints. The smallest number that can be represented is stored in Number.MIN_VALUE and the largest number is stored in Number.MAX_VALUE. If a calculation results in a number that cannot be represented, the number automatically gets the special value of Infinity.

var result = Number.MAX_VALUE + Number.MAX_VALUE;
alert(isFinite(result)); //false

NaN

There is a special numeric value in JavaScript called NaN, short for Not a Number, which is used to indicate when an operation intended to return a number has failed.

alert(isNaN(NaN)); //true
alert(isNaN(10)); //false - 10 is a number
alert(isNaN("10")); //false - can be converted to number 10
alert(isNaN("blue")); //true - cannot be converted to a number
alert(isNaN(true)); //false - can be converted to number 1

 

Number Conversions

There are three functions to convert nonnumeric values into numbers.

  • Number() function - It can be used on any data type
  • parseInt() function - Use for for converting strings to numbers
  • parseFloat() function - Use for for converting strings to numbers
var num1 = Number("Hello world!"); //NaN
var num2 = Number("000011"); //11
var num3 = Number(true); //1
var num1 = parseInt("1234blue"); //1234
var num2 = parseInt(""); //NaN
var num4 = parseInt(22.5); //22
var num5 = parseInt("70"); //70 - decimal
var num1 = parseFloat("1234blue"); //1234 - integer
var num3 = parseFloat("22.5"); //22.5


The String Type

var firstName = "Ryan"; // using doublequotes
var lastName = 'Dhungel'; // using sigle quotes

The length of any string can be returned by using the length property.

console.log(firstName.length); // 4

Converting to String

var num = 10;
alert(num.toString()); // "10"
var value1 = 10;
var value2 = true;
var value3 = null;
var value4;
alert(String(value1)); // 10
alert(String(value2)); // true
alert(String(value3)); // null
alert(String(value4)); // undefined

 

The Object Type

Objects in JavaScript are a groups of unordered data and functionality. Objects are created by using the new operator followed by the name of the object type to create.

var o = new Object();
All of the properties and methods of the Object type are also present on other, more specific objects.

 

Each Object instance has the following properties and methods:

  • constructor — The function that was used to create the object.  The constructor is the Object() function.
  • hasOwnProperty(propertyName) — Indicates if the given property exists on the object instance (not on the prototype). The property name must be specified as a string. o.hasOwnProperty("name")
  • isPrototypeOf(object) — Determines if the object is a prototype of another object.
  • propertyIsEnumerable(propertyName) — Indicates if the given property can be enumerated using the for-in statement. As with hasOwnProperty(), the property name must be a string.
  • toLocaleString() — Returns a string representation of the object that is appropriate for the locale of execution environment.
  • toString() — Returns a string representation of the object.
  • valueOf() — Returns a string, number, or Boolean equivalent of the object. It often returns the same value as toString().


Back to articles