Background
[wp_ad_camp_1]
Given an array of strings, we want to know if a particular string value is in that array. Here we’ll use the Array object’s .indexOf function.
Software Environment
- Any modern web browser
JavaScript Codes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var userIds = ['123ABC', '456DEF', '789GHI', '011JKL']; if(userIds.indexOf('456DEF') > -1) { // Value is in the array } Sometimes, we retrieve arrays from other codes. We may check first if the array is truly an Array or is not undefined. var userIds = service.getResponseDataFromServer(); if(userIds && Array.isArray(userIds)) { if(userIds.indexOf('456DEF') > -1) { // Value is in the array } } |
[wp_ad_camp_2]