Could someone explain why "" != null?

#test is a text field.

Each time I focusout of it. I run.

$("#test").val() == null
$("#test").val() == ""
$("#test).val()

With console.log wrapped around. The weird thing is, that if I leave the field empty.

item == null returns false
item == "" returns true
and item returns nothing (seems like null to me).

Why is “empty value” not null? Isn’t that exactly what it was since invention of first programming languages?

This comes from my project. Where I tried to find out if field is empty and I checked against null, where it failed, then I replaced it with "" (nothing) and it started working as intended… why does null not work?

not sure what you do, but it works for me.

console.log( $("#test").val() == null ); // true

I’m not really into javascript, but in php "" is setting the variable to an empty string, with emphasis on “is setting”.
When a value is NULL it is not set to anything.
An empty string is not nothing, it is an empty string.

1 Like

@SamA74 is right.

When you write this

let itemString = ''

You are basically assigned the string to variable, even if it’s empty - so that’s empty string, and for sure it would return it as false.

1 Like

The difference comes on how its handled in memory by the interpreter. So when creating a variable what you are really doing is assigning a memory address for the variable. And when you assign a value to that variable you are effectively creating another memory address for that value. But when you do not add a value or the value is NULL the memory address for the value is never created. So even if you assign a blank char or string you are still creating the memory address and in most languages instead of checking for the value NULL in memory its easier and faster to check if the memory address exists without ever knowing the value is NULL. But understand not all interpreters do this the same and sometimes they check the value and an empty string can equal NULL this is all in how the interpreter handles the parsing and the memory management of javascript. I know this doesn’t seem relevant to your question but understanding how variables, arrays, function are handled in memory can help you alot in the future. Plus its always nice to learn this stuff as you can optimize your code to handle better in memory.

So this is effectively the same thing;

var one;
var two = null;

I can never remember the differences in the ways PHP, JavaScript and MySQL decide what’s “truthy” and what’s “falsey” and always need to check the documentation.

Then there’s “datatype coercion to boolean” and strict vs. loose comparison involved too.

Not quite - if you declare a variable without assigning a value, its value will be undefined.

@fretburner Yes you are correct that is a caveat in javascript i tend to overlook the everything is an object crap. But what i was refering to is how the interpreter most of the time in many languages uses just memory to check the value. Since null is a primitive type object in javascript undefined and null are different in terms of how memory see’s them that is a great point.

[quote=“jgetner, post:8, topic:240030, full:true”]Since null is a primitive type object in javascript undefined and null are different in terms of how memory see’s them that is a great point.
[/quote]

How does that relate to the question at hand though?

When you do not provide a comparison, the condition checks to see if the boolean result is true or false.

if ($("#test").val()) { // Boolean("") equals false
    ...
}

In JavaScript, we talk of values being truthy or falsy, which speaks of what a value will be evaluated as when it is passed through a boolean condition.

Several types of values are considered to be falsy:

if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ('')
if ("")

Be wary though, for even though 0 and “” are falsy, “0” is not falsy as it contains a string character.

The double equals performs a loose equality comparison. Some loose equality charts have been created, but it can be difficult to fully understand the comparisons when coding.

For a good example, see the chart at http://dorey.github.io/JavaScript-Equality-Table/
http://www.jqueryscript.net/images/collective/Equality%20Table.jpg

Or for more complete details, see the chart at http://dorey.github.io/JavaScript-Equality-Table/unified/

Due to difficulties of remembering the above, it’s best to always use the triple-equals strict comparison instead, so that there is no doubt over what is being compared.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.