最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Null vs blank in JavaScript - Stack Overflow

programmeradmin1浏览0评论

I have attached jsfiddle example here. I want to know difference between null and blank

My function is working fine but when I change blank to null in if condition it stop working.

I have attached jsfiddle example here. I want to know difference between null and blank

My function is working fine but when I change blank to null in if condition it stop working.

Share Improve this question edited Aug 30, 2012 at 7:09 sp00m 48.9k31 gold badges150 silver badges259 bronze badges asked Aug 30, 2012 at 7:05 JitenderJitender 7,98932 gold badges116 silver badges218 bronze badges 5
  • 1 Blank (i.e. "") = value (but an empty one), null = no value at all. – sp00m Commented Aug 30, 2012 at 7:07
  • If you test it: '' != null. – Blender Commented Aug 30, 2012 at 7:09
  • since i have no value in first input why null is not working – Jitender Commented Aug 30, 2012 at 7:09
  • @amit You do have a value, just an empty one. More obvious is the difference between 0 and null, one is an actual value 0 and null is "I have no value". You can add a number to 0 but you can't add a value to no value. – Joachim Isaksson Commented Aug 30, 2012 at 7:10
  • @Joachim:so when should i use null in function – Jitender Commented Aug 30, 2012 at 7:12
Add a ment  | 

3 Answers 3

Reset to default 3

You have to pay attention to what "", null, and undefined, mean in javascript. It can lead to some misconception in boolean tests concerning values and types.

"" is the empty string which is used also as an empty property. A string object "" is defined and is not null.

null is an object whose type is null. An object is said to be null when it has no other value/property (its type is null).

undefined means that the object is not defined and the interpreter has no reference to it. undefined is not an object.

Some problems arises when you try to pare those values with false. Try to have a look at the following examples.

Here are some reference links:

Tutorial on some type checking

A little more explanation

  1. you should probably change the condition to if('input[type=radio]:checked').val()) this way it will check both null and empty strings. but in this case i dont think you will ever get a null string.

  2. dont use "!=" here because that might lead to some unpredictable behaviour use "!==".

  3. null probably is like a null pointer when the object actually points to nothing
  4. where as empty string points to an object which is ""

A NULL value represents the absence of a value for a record in a field.

An empty value is a value with no significant data in it.

Why an "empty character string" is different from a null value? Cause you're paring a string (with no characters inside) with a with a no data type value: they differ !

发布评论

评论列表(0)

  1. 暂无评论