Skip to main content

Featured post

How to Insert line break in Text component in React Native

Hi, I was developing an application in react native.I have to handle lots of text data in this small react native application.I have used  Text component in react native many times. While working on this application,i had a requirement to display some text content with line breaks.I will explain it clearly with an example.I want to display a text as shown below  Welcome Please subscribe my channel I wrote react native code as shown below <View> <Text> Welcome Please subscribe my channel </Text> <View> When i run these lines of code,It will display text as shown below Welcome Please subscribe my channel   - instead of displaying in two lines with line break,it displays in a single line.So i have to find a solution to insert line break in Text component in React Native.After lots of research,i found a clean solution as shown below <View> <Text> Welcome{"\n"} Please subscribe my channel </Text> <View> Here ...

How to check a variable is an array in javascript

In programming , some times we need to identify the given variable is array or not  in order to use that variable in our programming logic. Because we are handling different types of variables in our daily coding life. In this article I am going to explain ,How we can detect the given variable is array in javascript



Using instanceof operator


const array1 = [1,2,3,4];

console.log(array1 instanceof Array)  //true



let array2;

console.log(array2 instanceof Array)  //false



const obj = {};

console.log(obj instanceof Array)  //false










when we pass array1,array2 and obj to instanceof operator ,we can identify the variable type as shown above code. array1 is  an array variable  and it shows  true value  .But array2 and obj are not array variables  so  it shows false values 


Using Array.isArray() method

 

const array3 = [1,2,3,4];
console.log(Array.isArray(array3))  //true 


const array4 = null;
console.log(Array.isArray(array4))  //false


const num = 100;
console.log(Array.isArray(num))  //false







We are calling Array.isArray() method  with array3 , array4 and nums variables. But array3 shows true value .But array4 and num variables shows false values.So as explained above,we can use Array.isArray() and instanceof operator  to identify the given variable is array or not.