Core JavaScript

Intermediate JavaScript

JavaScript Reference


JavaScript Data Types with Examples


ECMAScript represents JavaScript Data types. This lesson provide JavaScript Data Types With Examples. How to define variable in JavaScript. JavaScript is a loosely data type dynamic language.

JavaScript, also known as ECMAScript specifies six primitive data types and object type.

JavaScript primitives data types,

and Object data types,

JavaScript Variables are declared using var statement. you can declare multiple variables at once. No need to append data type when you declare JavaScript variable. It means any value can store in any variable.

If you not initialize your variable in var statement, It's automatically assume values is undefined.


JavaScript Number Data types

JavaScript has only one Number (numeric) data types. Number data type can store normal integer, floating-point values.

A floating-point represent a decimal integer with either decimal points or fraction expressed (refer to another decimal number).

var num1 = 5;				// Numeric integer value
var num2 = 10.5;			// Numeric float value
var num3 = -30.47;			// Negative numeric float value

var num4 = 5E4;				// 5 x 10 Powers of 4 = 50000
var num5 = 5E-4				// 5 x 10 Powers of -4 = -50000

var num6 = 10 / 0;			// Number divide by zero, result is: Infinity
var num7 = 10 / -0;			// Number divide by negative zero, result is: -Infinity

var num8 = 10, num9 = 12.5; // Multiple variable declare and initialize

Run it...   »


JavaScript String Data types

JavaScript string data type represent textual data surrounding to single/double quotes. Each character is represent as a element that occupies the position of that string. Index value 0, starting from first character of the string.

var name = 'Hello, I am run this town.!';	// Single quote
var name = "Hello, I am run this town.!";	// Double quote

Whatever quote you use to represent string, but you should take care that quote can't repeat in string statement.

var name = "Hello, I'm run this town.!";	// Single quote use inside string
var name1 = ""; 							// empty string

Note : JavaScript empty string is different from the NULL value.


JavaScript Boolean Data types

JavaScript Boolean type can have two value true or false. Boolean type is use to perform logically operator to determine condition/expression is true.

var val1 = true;
var val2 = false;

JavaScript Symbol Data types

JavaScript Symbol data type new (Currently ECMAScript 6 Drafted) used for identifier unique object properties.

Symbol([description])

Parameters : description is optional for identify unique symbol.

var s1 = Symbol();
var s1 = Symbol('name');

JavaScript Null

JavaScript Null specifies variable is declare but values of this variable is empty.

var str = null;							// Value assign null
document.writeln(str == undefined);			// Returns true

document.writeln(null == undefined); 		// Equality check Returns true
document.writeln(null === undefined); 		// Equality with type check Returns false

Run it...   »


JavaScript empty variables boolean context return false.

var bool = Boolean();
console.log(bool);						// Default Boolean context Returns false

var bool = Boolean(true);
console.log(bool);						// Returns true

JavaScript undefined

JavaScript uninitialized variables value are undefined.

Uninitialized variable (value undefined) equal to null. JavaScript uninitialized variables boolean context return false.

var str;						// Declare variable without value. Identify as undefined value. 
document.writeln(str == null); 		// Returns true
document.writeln(undefined == null);	// Returns true
	
var bool = Boolean(str);		// str is undefined passed into Boolean object
document.writeln(bool);				// Boolean context Returns false

Run it...   »



JavaScript Object Data types

JavaScript all values are objects except primitive value. Object is store complex variable value, properties, method, functions. JavaScript Object referred to a memory for storing object values.

JavaScript Array

JavaScript Array is a collection of values that are belonging to a same data type.

Array values must be belonging opening or closing bracket.

Array value separated by comma.

Array values you can access by array indexing. Index value 0, starting from first value of the array.

var myarray = [];							// Create empty array.
var vowels = ["a", "e", "i", "o", "u"];		// Create array with values

document.writeln('vowels[0] ', vowels[0]);  		// Access first value
document.writeln('vowels[4] ', vowels[4]);  		// Access last value

Run it...   »


In JavaScript, objects (which are essentially associative arrays) create for mapping between keys and values pairs. Array keys is an index to access array values.

var users = [
	{"Name":"Opal Kole", "Address":"63 street Ct."}, 
	{"Name":"Max Miller", "Address":"41 NEW ROAD."}, 
	{"Name":"Beccaa Moss", "Address": "2500 green city."},
	{"Name":"Paul Singh", "Address": "1343 Prospect St"}	
];

console.log(users[0]);			 // index 0, object (1st record) return
console.log(users[0]["Name"]);   // index 0, Key: Name value return

Run it...   »


Notes : JavaScript does not support multidimensional arrays. But you can archive Multi-dimensional arrays represented by arrays of arrays.

var users = [
    [ 
      { "Name":"Opal Kole", "Address":"63 street Ct." }, 
      { "Name":"Max Miller", "Address":"41 NEW ROAD." } 
    ],
    [ 
      { "Name":"Beccaa Moss", "Address": "2500 green city." },
      { "Name":"Paul Singh", "Address": "1343 Prospect St" } 
    ]
];

console.log(users[0]);      		// users index 0, 1st Array Object return
console.log(users[0][0]);   		// users index 0, 1st Array Object (1st Record) return  
console.log(users[0][0]["Name"]);   // users index:0, 1st Array Object (1st Record), Key: Name value return

Run it...   »


JavaScript JSON

JSON stands for JavaScript Object Notation for storing data. Access data in any language platform.

JSON data is text data, but JavaScript JSON.parse() function to convert the JSON string into a JavaScript object.

JS JSON Object

JavaScript - JSON Object


var JSONText = '{ "users" : [{"Name":"Opal Kole", "Address":"63 street Ct."}, {"Name":"Max Miller", "Address":"41 NEW ROAD."}, {"Name":"Beccaa Moss", "Address": "2500 green city."},{"Name":"Paul Singh", "Address": "1343 Prospect St"}]}';

var jsondecode = JSON.parse(JSONText);

console.log(jsondecode["users"]);
console.log(jsondecode["users"][0]);
console.log(jsondecode["users"][0]["Name"]);

Run it...   »


JavaScript Function, Object and Properties

Here are brief explain about JavaScript function, object types, later chapter learn more about JavaScript function, object.

JavaScript function is a number of statements that performing some task and return the resulted values. function keyword you must have to specifies when you write function. JavaScript function you can define 3 way,

  • Simple Function: simple call the JavaScript function.
    <input type="button" value="Simple function" onClick="msg();"/>
    
    function msg(){ 
        alert("hello world!"); 
    }

    Run it...   »

  • In-line Function: In some special case for using in-line function to assign function to a variable at run-time instead of the parse-time.
    <input type="button" value="In-line function" onClick="msg();"/>
    
    var msg = function(){ 
        alert("hello world!"); 
    }

    Run it...   »

  • Anonymous Function: Anonymous function call either runtime or parse time. anonymous and in-line function both are working same.
    var msg;
    (function(msg){ 
        alert("hello world!"); 
    })(msg);

    Run it...   »


JavaScript object store complex variable value, properties, method. JavaScript Object referred to a memory for storing object values.

var myObj = new Object();										// Declare object
var myObj = new Object;											// Declare object
var myObj = new Object( [ param1, param2, ..., paramN ] );		// with parameter

myObj.propertyname;												// Access property
myObj["propertyname"];											// Access property

new operator to create an instance of the object. Object: required, for constructor of the object. parameter is optional.

var myObj = new Object(); 			// or you can write: var myObj = new Object;
myObj.name = "Opal Kole";			// Assign object property
myObj.address = "63 street Ct.";
myObj.age = 48;
myObj.married  = true;

console.log(myObj); 				// print object data

Run it...   »