Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In javascript, to check a variable contain a valid data or not, we can use the below code.
- var value;
- if(value){
- // do something
- }
- If the variable doesn't contain a valid value it may have following 6 type of content.
- This checking will valid if the variable is already defined.
- null
- undefined
- NaN
- empty string ("")
- 0
- false
- if the value is an array you need to check the length of value is not equal to zero.
- (value.length != 0).
- The safest way to check the variable is valid.
- if (typeof value != 'undefined' && value) {
- // Do something
- }
Add Comment
Please, Sign In to add comment