How to check if a variable is a string in JavaScript

2 Answers

0 votes
function isString(value){
   if (typeof value === 'string' || value instanceof String) {
       return true;
   }
   
   return false;
}
 
 
const s = "javascript";
const n = 84392;
 
console.log(isString(s)); 
console.log(isString(n)); 
 
console.log(isString('351')); 
    
        
        
        
/*
run:
    
true
false
true
    
*/

 



answered Jun 14, 2021 by avibootz
edited May 18, 2024 by avibootz
0 votes
const value = 'javascript';
  
if (typeof value === 'string' || value instanceof String) {
    console.log('string');
} else {
    console.log('not string');
}

    
    
    
/*
run:
    
string
    
*/

 



answered May 18, 2024 by avibootz

Related questions

1 answer 138 views
1 answer 146 views
1 answer 207 views
1 answer 145 views
...