- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
JavaScript isArray(): Check if an Object is an Array or Not
The JavaScript isArray() method is used to check whether a specified object is an array or not. For example:
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> const countries = ["Luxembourg", "USA", "Australia", "UK", "USA", "Canada"]; let x = Array.isArray(countries); if(x) document.getElementById("xyz").innerHTML = "'countries' is an Array."; else document.getElementById("xyz").innerHTML = "'countries' is not an Array."; </script> </body> </html>
In the above example, the first line
<!DOCTYPE html>
is the document type declaration for an HTML file.
The code within the <html> and <body> tags is the HTML markup for creating a web page. The <p> tag creates a paragraph element with an id attribute of "xyz".
The JavaScript code starts with the <script> tag and ends with the </script> tag.
The const keyword is used to declare a constant variable named countries. It is an array of strings containing the names of several countries.
The Array.isArray() method is used to determine whether the countries variable is an array. This method's return value is saved in a variable called x.
The code then uses an if-else statement to check the value of x. If it is true, it means that countries is an array, so the text "'countries' is an Array." is set as the inner HTML of the paragraph element with the id "xyz" using the document.getElementById() method. Otherwise, the text "'countries' is not an Array." is set as the inner HTML of the same paragraph element.
JavaScript isArray() syntax
The syntax of the isArray() method in JavaScript is:
Array.isArray(object)
Note: The Array before isArray() is necessary because of the static property.
The isArray() method returns true if the specified object is an array; otherwise, it returns false. For example:
<!DOCTYPE html> <html> <body> <p id="abc"></p> <script> let myVar = "codescracker.com"; document.getElementById("abc").innerHTML = Array.isArray(myVar); </script> </body> </html>
This example checks whether a variable myVar is an array or not using the Array.isArray() method in JavaScript. The Array.isArray() method returns true if the passed-in argument is an array, and false if it is not an array.
In this code, the myVar variable is assigned a string value "codescracker.com". The code then uses the document.getElementById() method to select the paragraph element with the id attribute "abc".
The innerHTML property of the paragraph element is then set to the result of Array.isArray(myVar), which will be false since myVar is not an array.
Therefore, when this code is executed, the text "false" will be displayed inside the paragraph element with the id attribute "abc".
« Previous Tutorial Next Tutorial »