Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

JS Array

Uploaded by

Rohan Jha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

JS Array

Uploaded by

Rohan Jha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Array

An array is an object that can store a collection of items. Arrays become really useful
when you need to store large amounts of data of the same type.
Suppose you want to store details of 500 employees. If you are using variables, you
will have to create 500 variables whereas you can do the same with a single array.
You can access the items in an array by referring to its indexnumber and the index
of the first element of an array is zero.
You can create an array in as given below.
var students = ["John", "Ann", "Kevin"];
You can also create an array using Array constructor like this:
var students = new Array ("John", "Ann", "Kevin");
JavaScript Array Methods
The Array object has many properties and methods which help developers to handle
arrays easily and efficiently. You can get the value of a property by specifying
arrayname.property and the output of a method by specifying arrayname.method().

1. length property –> If you want to know the number of elements in an array,
you can use the length property.
2. prototype property –> If you want to add new properties and methods, you
can use the prototype property.
3. reverse method –> You can reverse the order of items in an array using a
reverse method.
4. sort method –> You can sort the items in an array using sort method.
5. pop method –> You can remove the last item of an array using a pop method.
6. shift method –> You can remove the first item of an array using shift method.
7. push method –> You can add a value as the last item of the array.

In 2015, JavaScript introduced an important new keyword: const.

It has become a common practice to declare arrays using const:

(The keyword const is a little misleading. It does NOT define a constant array.
It defines a constant reference to an array. Because of this, we can still change the
elements of a constant array.)

<html>
<body>
<script>
const cars = ["XUV", "BREEZA", "BMW"];
document.write(cars);
</script>
</body>
</html>
Data type

JavaScript provides different data types to hold different types of values. There are
two types of data types in JavaScript.

1. Primitive data type


2. Non-primitive (reference) data type

JavaScript is a dynamic type language, means you don't need to specify type of the
variable because it is dynamically used by JavaScript engine. You need to
use var here to specify the data type. It can hold any type of values such as numbers,
strings etc. For example:

1. var a=40;//holding number


2. var b="Rahul";//holding string
javascript primitive data type

Data Type Description

String represents sequence of characters e.g. "hello"

Number represents numeric values e.g. 100

Boolean represents boolean value either false or true

Undefined represents undefined value

Null represents null i.e. no value at all

Javascript non-primitive data type


The non-primitive data types are as follows:

Data Type Description

Object represents instance through which we can access members

Array represents group of similar values

RegExp represents regular expression

Global Variable Local Variable

Global variables are declared outside Local Variables are declared within a function
all the function blocks. block.

The scope remains throughout the The scope is limited and remains within the
program. function only in which they are declared.

Any change in global variable affects Any change in the local variable does not affect
the whole program, wherever it is other functions of the program.
being used.

A global variable exists in the A local variable is created when the function is
program for the entire time the executed, and once the execution is finished, the
program is executed. variable is destroyed.

It can be accessed throughout the It can only be accessed by the function


program by all the functions present statements in which it is declared and not by the
in the program. other functions.

If the global variable is not initialized, If the local variable is not initialized, it takes the
it takes zero by default. garbage value by default.

Global variables are stored in the data Local variables are stored in a stack in memory.
segment of memory.

We cannot declare many variables We can declare various variables with the same
with the same name. name but in other functions.

Function closure

A closure can be defined as a JavaScript feature in which the inner function has access
to the outer function variable. In javascript, every time a closure is created with the
creation of a function.

The closure has three scope chains listed as follows:

o Access to its own scope.


o Access to the variables of the outer function.
o Access to the global variables.

You might also like