1

I am getting type error string is null,

i am checking string like this:

if (typeof s != 'undefined' || s.length){
        var j = JSON.stringify(
            s.split(',').reduce(function(m,v) {
               var t = v.split(':');
               m[t[0]] = t[1];
               return m;
            }, {})
         );
}

But still it shows type error, tell me how to not run this function on null ?

5
  • Where are you getting the error? Commented Aug 17, 2013 at 8:31
  • because s is null :-), how to check if s is not equal to `null Commented Aug 17, 2013 at 8:32
  • 1
    if (s) should be good enough to check that it's not null. Commented Aug 17, 2013 at 8:34
  • you tryed with this syntax typeof(s) !== 'undefined'? Commented Aug 17, 2013 at 8:35
  • @HanletEscaño, error was gone. Please post your comment as answer so that I can select it as answer Commented Aug 17, 2013 at 8:37

2 Answers 2

1

first of all null and undefined are two distinct values, so for the short circuiting to work you need to test both undefined and null you wouldn't need the typeof function for that you could simply do

if(s != undefined && s != null && s.length)

that changes the logic in your code because you are testing with || but that will always fail in your case (it will only be used if the type of s is undefined)

however since both null and undefined are falsy values. That is used as a boolean expression they both evaluate to false you can simply do

if(s && s.length){
}

There's quite a few falsy values in JS

  • null
  • undefined
  • "" //empty string
  • 0
  • false

The last one is the only truely false value so if you do value === false only the last of those five will evaluate to true where as all of them will evaluate to false in a condition

Any string that has a length is also a truthy value there's one somewhat surprising value that evaluates to true which is "false" and since all strings with a length > 0 will evaluate to true the only way the above if will fail is if there's no length property. That's is if it's not a string (or an array or any other type with a length property).

So you could either remove the test of length altogether

if(s){
}

or make it explicit that you are testing for a string

if(s && typeof s === "string"){
}
Sign up to request clarification or add additional context in comments.

2 Comments

s != null will it work? cause I heard that null is not equal to anything not even null
@user007 Just try it in your console in your browser write null != nullthat will answerthe question (the answer is yes it will work) null semantics vary from language to language E.g. in SQL your assumption would be correct in JS,Java,c++ or C# your assumption is not correct. SQL is more true to the original null semantics where null is "I don't know the value" and logically if you compare too things but you don't knwo what they are the it follows that you don't know if they are equal or not (I.e. the result would be null) that's the original semantics but not the semantics in JS
1

To check for null string you should probably try this:

if(s)
{}

as far as for empty string, checking the length the way you are should be fine and probably faster than === or ==.

3 Comments

will it check for undefined ??
Undefined means it has not been set a value. If you initialize it you should not have to check for it, but I guess also checking for it wouldn't hurt.
@HanletEscaño your comment is not entirely accurate since it's possible to assign undefined and undefined is a value (that can actually be redefined!) so even though you have assigned to the variable you are not guaranteed that the value is not undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.