Sunday 9 December 2018

What are the data types supported by JavaScript

EcmaScript standard defines 7 data types. Out of these 7, six are primitive types.

Primitive Types
Below table summarizes the primitive types supported by JavaScript.

Type
Description
Possible Values
boolean
It used to represent true, false states.
true, false
null
Specifies the absence of an object value.
null
undefined
If you do not assign a value to the variable, then the value of the variable is undefined
undefined
Number
Represent an integer (or) floating point number.
At the time of writing this post, JavaScript supports, numbers between -(253 -1) and 253 -1
String
It is a collection of characters, used to represent textual data.
Any text data
Symbol
Symbols are unique and immutable.
Example
const symbol1 = Symbol();
const symbol2 = Symbol(123);
const symbol3 = Symbol("Hello");

Object type
An object is a collection of properties. If you have any complex data, you can represent it as object.

HelloWorld.js
var state = true;

var x = null;
var y;

var PI = 3.14;

var message = "Hello World";

var symbol1 = Symbol(123);

var employee = {
  "name" : "krishna", 
  "city" : "Bangalore",
    
    toString: function() {
      return "[name = " + this.name + ", city = " + this.city + "]";
    }
};

console.log("state : " + state);
console.log("x : " + x);
console.log("y : " + y);
console.log("PI : " + PI);
console.log("message : " + message);
console.log("symbol1 : " + symbol1.toString());
console.log("employee : " + employee.toString());

Output
state : true
x : null
y : undefined
PI : 3.14
message : Hello World
symbol1 : Symbol(123)
employee : [name = krishna, city = Bangalore]



Previous                                                 Next                                                 Home

No comments:

Post a Comment