javascript-the distinction between null, undefined, and empty strings

Source: Internet
Author: User

I. separate judgment

1 varA=NULL;2 //var a=undefined;3 //var a= ';4 //var a= ' DD ';5 if(!a&&typeofA = = ' object '){6Console.log (' A is null '))7}Else if(!a&&typeofA = = ' undefined '){8Console.log (' A is undefined ')9}Else if(!a&&typeofA = = ' string '){TenConsole.log (' A is an empty string ')) One}Else{ AConsole.log (' Other ') -}

Two. The project may also have null, undefined, empty strings, and other requirements for judging

1 //var a=null;2 //var a=undefined;3 //var a= ';4 varA= ' DD ';5 if(!a && (typeofA = = ' object ' | |typeofA = = ' undefined ' | |typeofA = = ' string ')){6Console.log (' A is null, undefined, one of the empty strings ')7}Else{8Console.log (' Other ')9}

Three. encapsulating a function for your own use is that better?

1 functionJudgedata (str) {2     if(!str && (typeofstr = = ' object ' | |typeofstr = = ' undefined ' | |typeofstr = = ' String ')){3         return false;4}Else{5         return true;6     }7 }8 //by returning false or true to determine if there is data9Console.log (Judgedata (' DD '));//trueTenConsole.log (Judgedata (NULL));//false OneConsole.log (Judgedata (undefined));//false AConsole.log (Judgedata ("));//false

javascript-the distinction between null, undefined, and empty strings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.