Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

20020116023 WD

JavaScript built in object


What is built in Object???
:-JavaScript sports a number of built-in objects that extend the flexibility of the
language. These objects are Date, Math, String, Array, and Object.
 Date Object
:-By default, JavaScript will use the browser's time zone and display a date
as a full text string

When we use following script:


<!DOCTYPE html>
<html>
<body>
<script>
const d = new Date();
alert(d);
</script>
</body>
</html>

We also use
new Date(year, month, day, hours, minutes, seconds, milliseconds)
ex. const d = new Date(2022, 9, 24, 10, 33, 30, 0);
Note: JavaScript counts months from 0 to 11:January = 0.December = 11.
20020116023 WD

When we use string for date also it will return date like
const d = new Date("October 22, 2000 12:12:00");

When a Date object is created, a number of methods allow you to operate


on it.
Date methods allow you to get and set the year, month, day, hour, minute,
second, and millisecond of date objects, using either local time or UTC
(universal, or GMT) time.

 Math object

The JavaScript Math object allows you to perform mathematical tasks on


numbers.

If you want to use pi value we use Math.PI; :-3.141592653589793

Math.E it will returns Euler's number

Math.SQRT2 it will returns the square root of 2

Math.SQRT1_2 it will returns the square root of 1/2

Math.LN2 it will returns the natural logarithm of 2

Math.LN10 it will returns the natural logarithm of 10

Also we have ,

Math.round(x) Returns x rounded to its nearest integer

Math.ceil(x) Returns x rounded up to its nearest integer

Math.floor(x) Returns x rounded down to its nearest integer

Math.sign() returns -1,0,1 if x is negative, null or positive


Math.pow(x, y) returns the value of x to the power of y
Math.sqrt(x) returns the square root of x
20020116023 WD

We also have math.sin() & math.cos() it will returns value between -1 to 1.

Math.min() and Math.max() can be used to find the lowest or highest value
in a list of arguments.
Math.min(0,22,45,-45,10,55); output is -45
Math.max(0,22,45,-45,10,55); output is 55

exp(x) Returns the value of E^x.

 String Object
Strings are useful for holding data that can be represented in text form.
Some of the most-used operations on strings are to check their length,
to build and concatenate them using the + and += string operators.

JavaScript strings are for storing and manipulating text.


You can use single or double quotes.
Example :-
let text = "Bhavik"; it will return :- Bhavik
String Length
To find the length of a string, use the built-in length property
let text = "Bahvik"; return 6.

When using the == operator, compare equality


if both string are same then it returns true O/W false .
When using the === operator, compare inequality
if both string are not same then it returns true O/W false .
20020116023 WD

 Array Object
An array is a special variable, which can hold more than one value
Creating an Array
const array_name = [item1, item2, ...];
example :- const cars = ["Maruti suzuki", "Volvo", "BMW"];

Accessing Array Elements


You access an array element by referring to the index number:
Example
const cars = ["Maruti suzuki", "Volvo", "BMW"];
let car = cars[0]; it returns Maruti Suzuki.

const cars = ["Maruti suzuki", "Volvo", "BMW"];


let cars = cars[1]; it returns Volvo.

Changing an Array Element

This statement changes the value of the first element in cars:


cars[0] = "Tata"; it returns array like :- Tata,Volvo,BMW.

The length Property


The length property of an array returns the length of an array (the
number of array elements).
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let length = fruits.length;
now above statement returns value 4.

Adding Array Elements


The easiest way to add a new element to an array is using the push()
method:
Example:-const cars = [“Tata", "Volvo", "BMW"];
cars.push("Kia");
it add elements in array and our arry look like:-Tata,Volvo,BMW,Kai.

You might also like