Array Functions in JavaScript
Array: An array is collection of Heterogeneous data types. Array we are using to store the multiple values. Array is the collection of the elements and each element is the combination of key and value.
JavaScrip is loosely typed language. That is the reason why we can store any type of elements. Array is an object that’s why we have to declare the arrays using new keyword.
Sample Scrip:
<script>
arr=new Array(10,20,30)
alert(arr[1])
</script>
arr=new Array(10,20,30)
alert(arr[1])
</script>
In the above script 10 index number is 0 and 20 index number is 1 as well as 30 index number is 2. If you place 2 instead of 1 it displays the 30.
Length: By using this property we can get the total numbers of elements of an array.
Sample Script :
<script>
arr=new Array(40,10,20,30)
alert(arr.length)
</script>
arr=new Array(40,10,20,30)
alert(arr.length)
</script>
Sample Script :
<script>
arr=new Array();
arr[0]=1()
arr['city']='hyd'
arr['lan']='php'
alert (arr['lan'])
</script>
arr=new Array();
arr[0]=1()
arr['city']='hyd'
arr['lan']='php'
alert (arr['lan'])
</script>
Forin: By using these looping statements we can read the all elements of an array.
<script>
arr=new Array()
arr[100]=10
arr['uname']='php notes'
arr[0]=1
for(i in arr)
{
alert(arr[1])
}
</script>
arr=new Array()
arr[100]=10
arr['uname']='php notes'
arr[0]=1
for(i in arr)
{
alert(arr[1])
}
</script>
Join: By using this function we can join the array elements using the input characters.
Sample Script :
<script>
arr=new Array(10,20,30)
str=arr.join("/")
alert(str)
</script>
arr=new Array(10,20,30)
str=arr.join("/")
alert(str)
</script>
Sample Script:
<html>
<body>
<script type="text/javascript">
var parents = ["Jani", "Tove"];
var children = ["Cecilie", "Lone"];
var family = parents.concat(children);
document.write(family);
</script>
</body>
</html>
<body>
<script type="text/javascript">
var parents = ["Jani", "Tove"];
var children = ["Cecilie", "Lone"];
var family = parents.concat(children);
document.write(family);
</script>
</body>
</html>
Contact: To combating two array elements and it returns a new array.
Sample Script :
<script>
arr=new Array(10,20,30)
arr1=new Array('PHP','JavaScript','HTML')
str=arr.concat(arr1)
alert(str[3])
</script>
arr=new Array(10,20,30)
arr1=new Array('PHP','JavaScript','HTML')
str=arr.concat(arr1)
alert(str[3])
</script>
Split: This function demands a string as array elements based on the input element types.
Sample Script :
<script>
str="welcome TO JavaScript"
arr=str.split('t')
alert(arr[1])
</script>
str="welcome TO JavaScript"
arr=str.split('t')
alert(arr[1])
</script>
Example Login Script :
<script>
function fun1()
{
if(document.getElementById("but1".value="Hide")
{
document.getElementById("div1").style.visibility="hidden"
document.getElementById("but1").value"show"
}
else
{
document.getElementById("div1").style.visibility="visible"
document.getElementById("but1").value"Hide"
}}
</script>
<input type="button" value="Hide" onclick="fun1" id="but1">
<div style="background-color:green"; width:200 id="div1">
USERNAME <input type="text">
<br>
Password <input type="password">
<input type="button" value="Login">
</div>
function fun1()
{
if(document.getElementById("but1".value="Hide")
{
document.getElementById("div1").style.visibility="hidden"
document.getElementById("but1").value"show"
}
else
{
document.getElementById("div1").style.visibility="visible"
document.getElementById("but1").value"Hide"
}}
</script>
<input type="button" value="Hide" onclick="fun1" id="but1">
<div style="background-color:green"; width:200 id="div1">
USERNAME <input type="text">
<br>
Password <input type="password">
<input type="button" value="Login">
</div>
0 comments:
Post a Comment