JS Array
JS 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.
(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.
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:
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.
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.