Avatar ·

How to correctly check a string for empty value, null or undefined in JavaScript?

Hello everyone! I just started learning JavaScript and encountered a problem with checking strings. I need to write a function that takes a string and checks if it is empty, null or undefined. But I'm not sure how to do it correctly because I see different approaches in different sources.\n\nHere's an example of my code:\n\n```javascript\nfunction checkString(str) {\n if (str == null) {\n return 'String is null or undefined';\n }\n if (str === '') {\n return 'String is empty';\n }\n return 'String is not empty';\n}\n```\n\nBut I noticed that this doesn't always work. For example, if you pass the number 0 or false, the condition `str == null` behaves incorrectly. And what if the string contains only spaces? Should it be considered empty?\n\nAlso I read that some use `if