Tuesday, March 9, 2021

What are data types in Javascript?

 A) Values in Javascript will have a type.


B) There are 8 basic data types in Javascript.


1) String


let name = 'Test';  


String in Javscript is surrounded by quotes. 

Quotes can single, double quotes.


2) Number


let n = 10;  // Number


3) Boolean


let checkName =  true;

let checkData = false;


4) "null" 


It contains only null value. 

In Javascript, null is not a reference to a non existing object or null pointer. 

It just represents empty or unknown value.


let a= null;


5) "undefined"


It also represent special type, here the variable is declared but not assigned any value as shown below,


let a;


6) bigint


Used to store integers value of arbitrary length.


bigint value is created by appending n to the end of an integer.


const = 11111111112222222223333333333344444444n;


7) object


a) object are used to store collections of data and more complex entities.


b) objects can be created using {...} with optional list of properties, a property is "key": "value" pair.


c) Creating an empty object,


let studentDetail = {};

let steudentDetail = new Object();



d) Creating an object with properties,


let studentDetail = {

"name": "Test",

"rollNo": 1,

"Full Name": "Test User"


};


e) To access the property values use dot notation or array notation in case of space in key name as shown below,


studentDetail.name  // dot notation


To access full name use array notation as it has space.


studentDetail[Full Name]   // array notation


f) Adding a value to property,


studentDetail.department = 'Computer Science';


g) We may add comma to last property as shown below.


let studentDetail = {

name: "Test",

rollNo: 1,

};


h) To access the keys use,


Object.keys(studentDetail)


i) Use JSON.stringify to convert object to string


var jsonStrng= JSON.stringify(studentDetail);


console.log(jsonStrng);


"{"name":"Test","rollNo":1,"Full Name":"Test User"}"


j) Use JSON.parse to convert string to object


Object = JSON.parse(jsonStrng);


{name: "Test", rollNo: 1, Full Name: "Test User"}



k) The below are also different kinds of object in Javascript,


Array

Date

Error


8) symbol

9) Date Object

Javascript Date object let us work with dates. They are created with the new Date() constructor.

const d = new Date();

Below are 4 ways to create a new date object:

A) new Date()

B) new Date(year, month, day, hours, minutes, seconds, milliseconds)

C) new Date(milliseconds)

D) new Date(date string)


No comments:

Post a Comment