JavaScript Array Iteration Methods

Array iteration methods operate on every array item. Array.forEach() The forEach() method Calls a function once for each array element. Example var txt = “”; var numbers = [4, 9, 16, 25]; numbers.forEach(myFunction); function myFunction(value, index, array) { txt = txt + item + “<br>”; } Try it Yourself » Note that the function takes 3 arguments: The item value The item index […]

JavaScript Array Methods

Converting Arrays to Strings The JavaScript method toString() converts an array to a string of (comma separated) array values. Example var fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; document.getElementById(“demo”).innerHTML = fruits.toString(); Result Banana,Orange,Apple,Mango Try it Yourself » The join() method also joins all array elements into a string. It behaves just like toString(), but in addition you can specify the separator: Example var fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; […]

JavaScript String Methods

String methods help you to work with strings. String Methods and Properties Primitive values, like “John Doe”, cannot have properties or methods (because they are not objects). But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties. String Length The length property returns […]