How to check a string data type is present in array using JavaScript ? Last Updated : 25 May, 2023 Comments Improve Suggest changes Like Article Like Report In JavaScript, an array is a collection of data that can be of the same or different type. If we have the array containing the data, our task is to identify if it is a string data type. In this article, we will learn how to use the typeof operator. Syntax: typeof value Note: The typeof operator returns a string indicating the type of the operand. Example 1: Input: const sample = "GeeksforGeeks";console.log(typeof sample); Output : string Example 2: Input: const sample = 369;console.log(typeof sample); Output : number Example 3: Input: const sample = true;console.log(typeof sample); Output : boolean Approach: Iterate over the whole array to check if each element is a string or we will do it using for loop.We will compare the type of iterator element with the 'string' data type using the if block.If block will execute if it encounters the string data type.When there is no string value present, else block will get executed. Example 1: The following code demonstrates the case where the string is present. JavaScript <script> // Declare the array const arr = [1,2,3,"Geeks",4]; // initialized flag to zero let flag = 0; // iterating over whole array element one by one for(let i of arr){ // checking element is of type string or not if(typeof i == 'string'){ console.log("String found"); flag = 1; } } // if flag remain same that means we didn't get the string. if(flag==0){ console.log("String not found"); } </script> Output: String found Example 2: The following code demonstrates the case where the string is absent. JavaScript <script> // Declare the array const arr = [1,2,3,4,5]; // initialized flag to zero let flag = 0; // iterating over whole array element one by one for(let i of arr){ // checking element is of type string or not if(typeof i == 'string'){ console.log("String found"); flag = 1; } } // if flag remain same that means we didn't get the string. if(flag==0){ console.log("String not found"); } </script> Output: String not found We can identify any data type using the typeof operator. We can use it for other data types like eg. numbers, boolean, etc. Approach 2: Using Array.some() and instanceof: Initialize the Array. Use Some method on the Array which checks the instance of each element and returns true if any string is present. Print Evaluated result. Example: The following code demonstrates the case where the string is present. JavaScript // Declare the array const arr = [1, 2, "GFG", 4, 5]; // initialized flag to true if string found let flag = arr.some(x => x instanceof String); // if flag remain same that means we didn't get the string. if (flag) { console.log("String not found"); } else { console.log("String found") } Output: String found Time complexity: O(N), Where N is the length of the Array. Auxiliary complexity: O(1), Because no extra space is used. Comment More infoAdvertise with us Next Article How to check a string data type is present in array using JavaScript ? abhishekaslk Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Check Object is an Array in JavaScript? There are two different approaches to check an object is an array or not in JavaScript.1. Using Array.isArray() MethodThe Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false. 1 min read Check if a Given String is Binary String or Not in JavaScript Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string.Example:Input: "101010"Output: True, bin 3 min read Check If An Array of Strings Contains a Substring in JavaScript Here are the different ways to check if an array of strings contains a substring in JavaScript1. Using includes() and filter() methodWe will create an array of strings, then use includes() and filter() methods to check whether the array elements contain a sub-string.JavaScriptconst a = ['Geeks', 'gf 4 min read How to Check if a Variable is an Array in JavaScript? To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript 2 min read JavaScript - How To Check Whether a String Contains a Substring? Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false oth 3 min read How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if 2 min read Check if a variable is a string using JavaScript Checking if a variable is a string in JavaScript is a common task to ensure that the data type of a variable is what you expect. This is particularly important when handling user inputs or working with dynamic data, where type validation helps prevent errors and ensures reliable code execution.Below 3 min read How to check an array is empty or not using jQuery ? In this article, we will check if an array is empty or not using jQuery. In JavaScript, arrays are a special type of object. If we use the typeof operator for arrays, it returns "object". We can use jQuery's isEmptyObject() method to check whether the array is empty or contains elements. The isEmpty 2 min read Check if an array is empty or not in JavaScript These are the following ways to check whether the given array is empty or not:1. The length Property - mostly usedThe length property can be used to get the length of the given array if it returns 0 then the length of the array is 0 means the given array is empty else the array have the elements.Jav 1 min read How to Check if a Value is a Number in JavaScript ? To check if a value is a number in JavaScript, use the typeof operator to ensure the value's type is 'number'. Additionally, functions like Number.isFinite() and !isNaN() can verify if a value is a valid, finite number.Methods to Check if a Value is a NumberThere are various ways to check if a value 3 min read Like