Guest User

Untitled

a guest
Jan 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. In javascript, to check a variable contain a valid data or not, we can use the below code.
  2.  
  3. var value;
  4. if(value){
  5. // do something
  6. }
  7.  
  8. If the variable doesn't contain a valid value it may have following 6 type of content.
  9. This checking will valid if the variable is already defined.
  10.  
  11. null
  12. undefined
  13. NaN
  14. empty string ("")
  15. 0
  16. false
  17.  
  18. if the value is an array you need to check the length of value is not equal to zero.
  19. (value.length != 0).
  20.  
  21.  
  22. The safest way to check the variable is valid.
  23. if (typeof value != 'undefined' && value) {
  24. // Do something
  25. }
Add Comment
Please, Sign In to add comment