0% found this document useful (0 votes)
23 views5 pages

Objects in Javascript

The document explains three ways to create objects in JavaScript: using the 'new' keyword, function templates, and object literals. It also covers built-in objects such as String, Boolean, Array, Math, and Date, detailing their methods and examples of usage. Each object type is illustrated with code snippets demonstrating their functionalities.

Uploaded by

ashikap.23bcr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

Objects in Javascript

The document explains three ways to create objects in JavaScript: using the 'new' keyword, function templates, and object literals. It also covers built-in objects such as String, Boolean, Array, Math, and Date, detailing their methods and examples of usage. Each object type is illustrated with code snippets demonstrating their functionalities.

Uploaded by

ashikap.23bcr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Objects in JavaScript

 There 3 ways to create an object:


1. Direct instance – new keyword
2. Function template – this key word
3. Object literal

 Direct Instance:

let a = new Object();

a.name="praga";

a.age=20;

a.email="[email protected]";

console.log(a);//Output:{ name: 'praga', age: 20, email: '[email protected]' }

console.log(a.name); //output: praga

 Object Literal:

let o = new Object();

o.name="priya";

o.age=25;

console.log(o.age); ///Output: 25

 Function Template

function color(c1,c2)

this.c1=c1;
this.c2=c2;

console.log(this.c2);

let func = new color("pink","blue"); //ouput: blue

Built-in –Objects in JavaScript

1. String Object
2. RexExp Object
3. Boolean Object
4. Number Object
5. Array Object
6. Math Object
7. Date Object

String Object

 A string a sequence of characters


 Methods:

charAt() – Returns character.

toUpperCase() – String value to upper case

toLowerCase()- String value to lower case

concat()- join two strings

 Ex:

let s = new String("Web Programming");

let b = new String(" Laboratory");

console.log(s.length); //Output: 15

console.log(s.toUpperCase()); //Output: WEB PROGRAMMING


console.log(s.charAt(2)); //Output:b

console.log(s.concat(b)); //Output: Web Programming Laboratory

Boolean Object:

 Convert non-boolean value to boolean values.


 Two values: True or False.
 Ex:

let age=16;

let c = Boolean(age>=16); // true

if(c)

console.log("true");

else

console.log("false");

Output: true

Array Object

 Used to store multiple values in a single variable.


 Ex1:

let arr = new Array("WP","C","DP");

let l = arr.length; //l=3


let i=0

while(i<l)

console.log(arr[i]);

i++;

Output:

WP

DP

 Ex2:

let arr = new Array("WP","C","DP");

let l = arr.length; / /l=3

console.log(arr.reverse()); //Reverse the given array O/P: [ 'DP', 'C', 'WP' ]

console.log(arr.push("CE")); //Add new element as last and returns length


O/P:4

console.log(arr.sort()); //Sorts the elements O/P: [ 'C', 'CE', 'DP', 'WP' ]

console.log(arr.pop("CE")); //Removes the last element O/P: WP

console.log(arr); // O/P: [ 'C', 'CE', 'DP' ]

Math Object

 Perform simple and complex arithmetic operations.


 Ex:
let pi=33.14;

console.log(Math.ceil(pi)); //Nearset larger integer O/P: 34

console.log(Math.floor(pi)); //Nearest smaller integer O/P: 33

console.log(Math.abs(pi)); //Absoulte value O/P: 33.14

console.log(Math.round(pi)); //Rounds the nearest integerO/P: 33

Date Object

 Used to display a date in a numerical or mathematical calculation.


 Ex:

let d = new Date();

console.log(d.getFullYear()); //2023

console.log(d.getDate()); //13

console.log(d.getMonth()); //ranges from (Jan-0 to Dec-11) 9-Oct

console.log(d.getDay()); //ranges from (Mon-0 to Sun-6) 5-Fri

You might also like