0% found this document useful (0 votes)
7 views6 pages

Arrays & Date

The document explains the differences between object-based programming and object-oriented programming, highlighting that JavaScript is object-based and does not support user-defined classes or inheritance. It covers the concept of objects, arrays, and native JavaScript classes, detailing how to create and manipulate arrays, including methods like concat, join, reverse, and sort. Additionally, it discusses the Date object, how to create it, and various methods to extract date information.

Uploaded by

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

Arrays & Date

The document explains the differences between object-based programming and object-oriented programming, highlighting that JavaScript is object-based and does not support user-defined classes or inheritance. It covers the concept of objects, arrays, and native JavaScript classes, detailing how to create and manipulate arrays, including methods like concat, join, reverse, and sort. Additionally, it discusses the Date object, how to create it, and various methods to extract date information.

Uploaded by

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

JavaScript – An Object Based Programming Language

1. Write the difference between object based programming and Object oriented programming?
Object based programming does not support user defined classes. It does not support the concept of
Inheritance also. JavaScript is an object based scripting language.
Object Oriented Programming supports Classes and Inheritance. Java and C++ are examples of
OOP.
2. What is an Object?
An Object is a programming abstraction that groups data with the code that operates on it. Objects
have its own properties and methods. An object can be created using the new keyword and its
constructor.
Constructor is a function with the same name of the class. Dot operator is used to access an
object’s property or method.
var ar=new Array( );
here ar is the object . Array( ) is the constructor
3. Name 3 Native JavaScript classes.

Array, Date, Window, Math

ARRAYS
1. What are scalar variables?
Variables that can be assigned with a single value is called as scalar variables.
A=10;
B=”hello”
2. What is an Array?
An array is a variable which can hold a group of values in it. They are built in Objects in java script.
Each value in an array is called as an Element. Each Element is referenced by an index or subscript.
If there are n elements in an array, their indices range from 0 to n-1
Use the following JavaScript statements to create an array
 var ar=new Array();
 var ar1=new Array(3);
 var marks=new Array(11,44,56);

To create an array object the new keyword is used along with the constructor (Array())

ar1 is an array object with an initial size of 3


ar2 is an array object with elements 11,44,56

11 44 56
marks[0] marks[1] marks[2]
Array&Date 1 AnupA
3. Explain the length property of an Array object
length returns the number of elements in the array
var names=new Array("ann", "arun", "cathy");
document.write(names.length)

output

4. Array methods

 concat(array2) – It concatenates 2 arrays and returns the new array. The method is used on the first
array and pass the name of the second array as its parameter.

Ar1 =new Array(11,44,55)


Ar2 =new Array(2,77,99,100)
document. write (Ar1.concat(Ar2))

output

11,44,55,2,77,99,100

 join ( delimiter) – It joins all the elements in an array into a single string separated by the given
delimiter

Ar1 =new Array (11,44,55)


document. write (Ar1.join("*"))

output

11*44*55

 reverse( ) - It reverses the order of the elements in the array

Ar1=new Array(11,44,55)
document. write (Ar1.reverse())

output

55,44,11

Array&Date 2 AnupA
 sort( ) – It sorts the elements in the array in ascending order according to the Unicode character

Ar1 =new Array(11,41,5, 25)


document. write (Ar1.sort( ) )

output

11, 25, 41, 5

Names=new Array ( “mahima”, “michelle”, “aaron”, “ann”)


document. write (Names.sort( ) )

output

aaron, ann, mahima, michelle

 tostring( ) – Converts the array elements into a string where the array elements are separated by
commas
output
Ar1 =new Array(11,41,5, 25)
document. write (Ar1.tostring( ) ) 11,41,5,25

5. Write a program to initialize an array marks with 5 subjects marks of a student. Find the total and
average marks of the student.

<SCRIPT language="JavaScript">
var marks = new Array(78.5, 81, 87,65.5, 93)
var total=0, avg=0;

for(i=0;i<ar.length;i++)
{
total=total+ ar [i];
}
avg= total/5;
document.write(“total marks=”+total+”<br>”);
document.write(“average marks=”+avg+”<br>”);

Array&Date 3 AnupA
for – in loop
 This loop works with Arrays or objects
 for ..in loop is similar to for loop but instead of repeating a loop for a number of
times, it repeats the loop for each element of a specified collection of items

<html>
<body>
<script>
var names=new Array ("ann", "ben", "cathy")

for(i in names)
{
document.write(names[i]+"<BR>");
}
</script>
</body>
</html>

Output
ann
ben
cathy

Array&Date 4 AnupA
Date Object
1. How to create a Date Object?
Date is a built in class in java script. A Date Object can be created using the new key
word and a constructor as follows
 To print the current date and time
var d1 =new Date( );
document. write(d1); //will print the current date and time

Wed Jan 02 2019 08:21:12 GMT+0300 (Arabian Standard Time)

 var d2=new Date(“January 1 2019”); //it will set only the date
document. write(d2);

Tue Jan 01 2019 00:00:00 GMT+0300 (Arabian Standard Time)

 var d3= new Date(year, month, date, hours, minutes, seconds, milliseconds);

var d3=new Date(2017,11,22,5,23,50)


document. write(d3); //month index starts from 0. 0 represents January

Fri Dec 22 2017 05:23:50

2. Methods of Date objects


var d=new Date(2017,12,22,5,23,50);
document.write (d.getDate( ) ); //Extracts the date from the object d
22
document.write (d.getDay( ) ); //returns the index of the day of the week
1 //0 for Sunday, 1 for Monday etc.
document.write (d.getMonth( ) );
0 //0 or 12 represents january

Array&Date 5 AnupA
 var d=new Date( ) // d will store the current date and time
document.write (d.getYear( ) ); //it counts from 1900
document.write (d.getFullYear( ) ); //retrieves the year as a four digit number
119
2019

 var d=new Date(2017,12,22,5,23,50);


document.write (d.getHours( )+","+d.getMinutes( )+","+d.getSeconds( ));

5, 23, 50

 var d =new Date(2017,12,22,5,23,50);


d.setYear(2018);
d.setMonth(3); //0 represents January, 3 represents April
d.setDate(11);
document.write(d);

Wed Apr 11 2018 05:23:50

 Write a JavaScript code to calculate the date 12 months from now and display it on
the web page
var d=new Date( );
document.write(d+"<br>")
d.setMonth(d.getMonth( )+12)
document.write(d)

Array&Date 6 AnupA

You might also like