Checking value from array javascript

Tutorial for Checking value from array javascript only. I found useful following URL working with Javascript Array.
http://www.javascriptkit.com/jsref/arrays.shtml

Here is my code to check value from array

var MyArrayVar = new Array(1, 2, 3, 4, 5);

testVar = 2;

if(MyArrayVar.indexOf(testVar) >= 0) {
alert("We got testVar in MyArrayVar");
}else{
alert("We did not got testVar in MyArrayVar");
}

As per this condition I am chekcing the What is returned by this MyArrayVar.indexOf(testVar).
You can check this by using “alert(MyArrayVar.indexOf(testVar));” line.
You will get this number value if testVar number or value present in Array.

You that value is not present in Array you will get the -1 result always.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.