JavaScript Array
JavaScript Array
{MOBILE NO:-+91-8275265361]
Deleting Properties
The delete keyword deletes a property from an object:
Example
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
delete person.age;
The delete keyword deletes both the value of the property and the property
itself.
After deletion, the property cannot be used before it is added back again.
JavaScript Arrays
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.
Example
var cars = ["Saab", "Volvo", "BMW"];
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single
variables could look like this:
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
However, what if you want to loop through the cars and find a specific one? And what if you
had not 3 cars, but 300?
An array can hold many values under a single name, and you can access the values by
referring to an index number.
Creating an Array
Using an array literal is the easiest way to create a JavaScript Array.
Syntax:
var array_name = [item1, item2, ...];
Example
var cars = ["Saab", "Volvo", "BMW"];
Spaces and line breaks are not important. A declaration can span multiple lines:
Example
var cars = [
"Saab",
"Volvo",
"BMW"
];
JavaScript Array
JavaScript array is an object that represents a collection of similar type of elements.
1. By array literal
2. By creating instance of Array directly (using new keyword)
3. By using an Array constructor (using new keyword)
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
var arrayname=[value1,value2.....valueN];
As you can see, values are contained inside [ ] and separated by , (comma).
Let’s see the simple example of creating and using array in JavaScript.
<script>
var emp=["Sonoo","Vimal","Ratan"];
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br/>");
}
</script>
<script>
var i;
var emp = new Array();
emp[0] = "Arun";
emp[1] = "Varun";
emp[2] = "John";
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
</script>
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
<script>
var emp=new Array("Jai","Vijay","Smith");
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
</script>
Example
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars[0];
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon"); // adds a new element (Lemon) to fruits
New element can also be added to an array using the length property:
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[fruits.length] = "Lemon"; // adds a new element (Lemon) to fruits
WARNING !
Adding elements with high indexes can create undefined "holes" in an array:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[6] = "Lemon"; // adds a new element (Lemon) to fruits
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
The elements will be separated by a specified separator. The default separator is comma (,).
JavaScript Functions
JavaScript functions are used to perform operations. We can call JavaScript function many times to reuse the
code.
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
Syntax
1. new Function ([arg1[, arg2[, ....argn]],] functionBody)
Parameter
arg1, arg2, .... , argn - It represents the argument used by function.
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
Method Description
apply() It is used to call a function contains this value and a single array of
arguments.
call() It is used to call a function contains this value and an argument list.
Example 2
Let's see an example to display the power of provided value.
<script>
var pow=new Function("num1","num2","return Math.pow(num1,num2)");
document.writeln(pow(2,3));
</script>
Functions are very important and useful in any programming language as they make the
code reusable A function is a block of code which will be executed only if it is called. If you
have a few lines of code that needs to be used several times, you can create a function
including the repeating lines of code and then call the function wherever you want.
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
Syntax:
function functionname()
<html>
<head>
<title>Functions!!!</title>
<script type="text/javascript">
function myFunction()
myFunction();
</script>
</head>
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
<body>
</body>
</html>
You can create functions with arguments as well. Arguments should be specified within
parenthesis
Syntax:
function functionname(arg1, arg2)
You can also create JS functions that return values. Inside the function, you need to use the
keyword return followed by the value to be returned.
Syntax:
function functionname(arg1, arg2)
return val1;
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
return sum;
}
var firstNo = 78;
var secondNo = 22;
document.write(firstNo + " + " + secondNo + " = " + returnSum(firstNo,secondNo));
</script>
</head>
<body>
</html>
Introduction
A string is a sequence of one or more characters that may consist of letters, numbers, or
symbols. Each character in a JavaScript string can be accessed by an index number, and
all strings have methods and properties available to them.
In order to test the difference between the two, we will initialize a string primitive and a
string object.
typeof stringPrimitive;
Output
string
In the second example, we used new String() to create a string object and assign it to
a variable.
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
typeof stringObject;
Output
Object
Most of the time you will be creating string primitives. JavaScript is able to access and
utilize the built-in properties and methods of the String object wrapper without
actually changing the string primitive you've created into an object.
While this concept is a bit challenging at first, you should be aware of the distinction
between primitive and object. Essentially, there are methods and properties available to
all strings, and in the background JavaScript will perform a conversion to object and
back to primitive every time a method or property is called.
To demonstrate, we will create a string with the value How are you?.
H o w a r e y o u ?
0 1 2 3 4 5 6 7 8 9 10 11
The first character in the string is H, which corresponds to the index 0. The last character
is ?, which corresponds to 11. The whitespace characters also have an index, at 3 and 7.
Being able to access every character in a string gives us a number of ways to work with
and manipulate strings.
1. charAt(x) Returns the character at the “x” position within the string.
//charAt(x)
console.log(myString.charAt(7));
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
//output: F
2. charCodeAt(x) Returns the Unicode value of the character at position “x” within the
string.
//charAt(position)
var message="jquery4u"
//alerts "q"
alert(message.charAt(1))
3. concat(v1, v2,…) Combines one or more strings (arguments v1, v2 etc) into the
existing one and returns the combined string. Original string is not modified.
//concat(v1, v2,..)
var message="Sam"
alert(final)
//fromCharCode(c1, c2,...)
console.log(String.fromCharCode(97,98,99,120,121,122))
//output: abcxyz
console.log(String.fromCharCode(72,69,76,76,79))
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
//output: HELLO
//(PS - I have no idea why you would use this? any ideas?)
5. indexOf(substr, [start]) Searches and (if found) returns the index number of the
searched character or substring within the string. If not found, -1 is returned. “Start” is
an optional argument specifying the position within string to begin the search. Default is
0.
//indexOf(char/substring)
if (sentence.indexOf("Sam")!=-1)
alert("Sam is in there!")
6. lastIndexOf(substr, [start]) Searches and (if found) returns the index number of the
searched character or substring within the string. Searches the string from end to
beginning. If not found, -1 is returned. “Start” is an optional argument specifying the
position within string to begin the search. Default is string.length-1.
//lastIndexOf(substr, [start])
console.log(myString.lastIndexOf('r'));
//output: 11
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
console.log(isInt);
//output: 999
console.log(isInt);
//output: null
//replace(substr, replacetext)
console.log(myString.replace(/JavaScript/i, "jQuery"));
//replace(regexp, replacetext)
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
9. search(regexp) Tests for a match in a string. It returns the index of the match, or -1 if
not found.
//search(regexp)
console.log(isInt);
//output: 0
console.log(isInt);
//output: -1
10. slice(start, [end]) Returns a substring of the string based on the “start” and “end”
index arguments, NOT including the “end” index itself. “End” is optional, and if none is
specified, the slice includes all characters from “start” to end of string.
//slice(start, end)
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
var text="excellent"
11. split(delimiter, [limit]) Splits a string into many according to the specified delimiter,
and returns an array containing each element. The optional “limit” is an integer that lets
you specify the maximum number of elements to return.
//split(delimiter)
var word=message.split("l")
12. substr(start, [length]) Returns the characters in a string beginning at “start” and
through the specified number of characters, “length”. “Length” is optional, and if omitted,
up to the end of the string is assumed.
//substring(from, to)
var text="excellent"
13. substring(from, [to]) Returns the characters in a string between “from” and “to”
indexes, NOT including “to” inself. “To” is optional, and if omitted, up to the end of the
string is assumed.
//substring(from, [to])
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
myString = myString.substring(0,10);
console.log(myString)
//output: javascript
14. toLowerCase() Returns the string with all of its characters converted to lowercase.
//toLowerCase()
myString = myString.toLowerCase();
console.log(myString)
15. toUpperCase() Returns the string with all of its characters converted to uppercase.
//toUpperCase()
myString = myString.toUpperCase();
console.log(myString)
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
String Properties
Property Description
String Methods
Method Description
charCodeAt(position) Returns a number indicating the Unicode value of the character at the given
position (in Number).
concat([string,,]) Joins specified string literal values (specify multiple strings separated by
comma) and returns a new string.
indexOf(SearchString, Returns the index of first occurrence of specified String starting from
Position) specified number index. Returns -1 if not found.
lastIndexOf(SearchString, Returns the last occurrence index of specified SearchString, starting from
Position) specified position. Returns -1 if not found.
match(RegExp) Search a string for a match using specified regular expression. Returns a
matching array.
replace(searchValue, Search specified string value and replace with specified replace Value string
replaceValue) and return new string. Regular expression can also be used as searchValue.
slice(startNumber, Extracts a section of a string based on specified starting and ending index
endNumber) and returns a new string.
split(separatorString, Splits a String into an array of strings by separating the string into substrings
limitNumber) based on specified separator. Regular expression can also be used as
separator.
substr(start, length) Returns the characters in a string from specified starting position through
the specified number of characters (length).
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
Method Description
substring(start, end) Returns the characters in a string between start and end indexes.
Method Description
link() Wraps a string in <a>tag where href attribute value is set to specified string.
VAPM,Almala-Inst.Code-1095
CSS[22519] MR.SWAMI R.S.{MOBILE NO:-+91-8275265361]
Method Description
VAPM,Almala-Inst.Code-1095