JS: Data Types | JavaScript

JavaScript Data Types

In computer science, data is the anything that is meaningful to computer. JavaScript Data Types can be categorized as:

  • Primitive Types
  • Trivial Types
  • Composite Types
var i = 0; var s = "PiTribe"; var b = true;
JavaScript is a Loosely and Dynamically typed programming language.

Primitive Types

  • Number
  • String
  • Boolean
  • undefined
typeof 1.2345; typeof true; typeof "PiTribe"; var u; typeof u; "number" "boolean" "string" "undefined"

number

implicit parsing

explicit parsing

JavaScript parsing functions return the parsed value in the respective data type.

parseInt()

var i = parseInt("3.142857142857143"); console.log(x);

In the example above, value parsed to Integer part i.e. 3.

parseFloat()

var pi = parseFloat("3.142857142857143"); console.log(pi);

In the example above, value parsed to double data type and returns 3.142857142857143.

undefined

undefined implies that the variable is virgin i.e. no value been yet set to the variable

var x; console.log(typeof x);
"undefined"

Trivial Types

  • null

null

Composite Types

  • Object
  • Array
typeof {a:1,b:2}; typeof [1,2,3]; typeof function(){}; typeof new Date(); "object" "object" "function" "object"

Object

An object stores multiple values in a single variable.

var o = {"continent":"Asia","countries":55,}; console.log(typeof o, o) console.log(typeof o.continent, o.continent); console.log(typeof o.countries, o.countries); object {continent: 'Asia', countries: 55} string Asia number 55

Array

JavaScript array within [ and ], store multiple values of any JavaScript data type, seperated by comma ,.

var arr = ["Earth", 7, 3.14, true, {"name":"Abc Xyz", id:12345}]

In the code above, "Earth" is a string, 7 is an integer number, 3.14 is a float number, true is the boolean value and {"name":"Abc Xyz", id:12345} is a JSON value.

  1. Data Type means what kind of information being stored in a program memory.
  2. JavaScript is a Loosely and Dynamically typed programming language