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()

The time is adjusted according to timezone the code is running in.

Example:

let now= new Date();

alert( now ); // shows current date/time

Output:

Sun Aug 07 2022 14:40:43 GMT+0530 (India Standard Time)

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

C) new Date(milliseconds)

D) new Date(date string)

The time is adjusted according to timezone the code is running in.

Example:

let date = new Date("2022-01-01");

alert(date);

Output:

Sat Jan 01 2022 05:30:00 GMT+0530 (India Standard Time)

For accessing the date components below methods are available,

A) getFullYear()

Returns 4 digits.

B) getMonth()

Get the month, Values return are from 0 to 11.

C) getDate()

Get the day of month, Values return are fromfrom 1 to 31.

D) getHours(), getMinutes(), getSeconds(), getMilliseconds()

To get the other corresponding time components.

Similarly we have set Methods as well.

Notes:

1) Date and time in Javascript are represented with the Date object. We cannot create "only date" or "only time".

2) Months are counted from 0 to 11. January is a 0 month.

3) Use Date.now() to get current time stamp.

alert(Date.now());

Timestamps in javascript are in milliseconds and not seconds.

No comments:

Post a Comment