2013-07-04 13 views
5
  1. ヌル
  2. 未定義
  3. 空の文字列

私はそれらを使用しますが、私が持っている上記のリストでマージナル違い各見通しのまだ気づいていませんよ。 私は主に0、falseを使用しています。しかし、私は未定義の空の文字列を使用する多くのスクリプトに出くわしました。 私はそれらの間の正確な差異を知りたいです。 私はその愚かな質問を知っていますが、私は小さなconssise答えを得る場合は素晴らしいでしょう。変数に与えられた代入の違いはありますか?

+0

「user2549366」の回答を参照してください。 –

+0

私はそのリンクよりも良い答えがあると思います。あなたの答えは –

答えて

0

タイプ違いです。

falsenullundefinedが未定義である、オブジェクトであり、''が文字列であり、0は数字、ブール値です。

タイプは、そのタイプとして使用されているものに基づいて選択します。たとえば:

// There is nothing wrong with this block of code 
var num_cats = 7; 
if(num_cats){ 
    // num_cats is truthy 
} 

// This block works but it could be made more clear 
var has_cat = num_cats; 
if(has_cat){ 
    // This would work, but it doesn't make sense. As a developer I would 
    // expect that has_cat should be either true or false, not 7. 
    // One way to convert the number to a boolean would be: 
     has_cat = !!num_cats 
} 

二つの最も混乱falsey値は、おそらくnullundefinedです。

nullは基本的には変数が存在することを意味し、それは値が不明なのです。変数が明示的にvar x = undefined;ようundefinedに設定することができ、その後、変数xが存在するが、明示的にそれが存在しないかのようにあなたはそれを扱うことができることを意味している定義されていないが undefinedは(変数が存在しないことを意味します。

2

あなたはそれを参照する方法を知りたい場合には、「truthyとfalsy値」と呼ばれています ここでは、あなたの質問に答えを説明するためのリンクがある:。http://www.sitepoint.com/javascript-truthy-falsy/ (その冒頭でリンクを読むときに覚えておいてください!!(値)trueまたはfalseのいずれかに値を強制的に)

+0

+1です。 –

0

2つだけあなたが言及した値のは、私が「特別指定呼ぶものです値 "とする。

  • null - ほとんどの場合、これは該当しません。
  • undefined - 両方のためのnull

例の暗黙のバージョン:

function findByTitle(arr, title) 
{ 
    for (var i = 0; i < arr.length; ++i) { 
     if (arr[i].title === title) { 
      return arr[i]; 
     } 
    } 
    return null; 
} 

nullの戻り値は、それ以外の場合は、オブジェクトの、レコードが見つからなかったことを示しています。この場合

function increment(x, by) 
{ 
    return x + (by || 1); // by may be undefined 
} 

increment(4); // 5 

JavaScriptが暗黙のうちundefinedとしてそれを渡すので、by引数は、渡されません。私はこれを変数に割り当てることを推奨しません。むしろ、nullを使用します。

あなたが言及した他の値は特別なものではありません。文字列値の作成や合計の計算などの開始値として使用できますが、それ自体は特別なものではありません。

  • ""
  • falseはブール
  • 0NaNはあなたがJavaScriptで6 "falsy" の値の5あるきリスト番号
0

ある文字列です。 "NaN"を追加すると、javascriptにすべての偽の値が含まれます。だから、完全な「falsy」リストは、「もし」ステートメントで使用する場合

1)0 
2)"" 
3)false 
4)undefined 
5)null and 
6)NaN 

あり、これらのすべてが同じように動作するので、

if(0) 
if("") 
if(false) 
if(undefined) 
if(null) and 
if(NaN) would behave the same 
あなたは短い簡潔な答えを求め

が、私は最高だと思います基本的なテストでどのように動作するかを示すだけです。長い答え

//Checking if all the "falsy" values evaluate the same way in a "if" statement 
console.log("Checking if(0)"); 
if(0) { 
    console.log(" if(0) Will not be reached"); 
} 

console.log('Checking if("")'); 
if("") { 
    console.log(' if("") Will not be reached'); 
} 

console.log("Checking if(undefined)"); 
if(undefined) { 
    console.log("if(undefined) Will not be reached"); 
} 

console.log("Checking if(null)"); 
if(null) { 
    console.log("if(null) Will not be reached"); 
} 

console.log("Checking if(Nan)"); 
if(NaN) { 
    console.log("if(NaN) Will not be reached"); 
} 

console.log("Checking if(false)"); 
if(false) { 
    console.log("if(false) Will not be reached"); 
} 

//チェックするための謝罪すべてfalsy値はif文はfalseとfalsy間の厳密に等しいかどうかを確認する

if(0 == "") { 
    console.log('if(0 == "") is true'); 
} 

if(0 == false) { 
    console.log("if(0 == false) is true"); 
} 

if("" == false) { 
    console.log('if("" == false) is true'); 
} 

if(0 == undefined) { 
    console.log("if(0 == undefined) Will not be reached"); 
} 

if("" == null) { 
    console.log('if("" == null) Will not be reached'); 
} 

if(undefined == null) { 
    console.log("if(undefined == null) is true"); 
} 

if(NaN == "") { 
    console.log('if(NaN == "") Will not be reached'); 
} 

//で互いに(==)等しい場合値 if(未定義=== false){ console.log( "到達しません");これが何を意味するのか }

if(null === false) { 
    console.log("Will not be reached"); 
} 

if(undefined ===false) { 
    console.log("Will not be reached"); 
} 

if(0 === false) { 
    console.log("Will not be reached"); 
} 

if("" === false) { 
    console.log("Will not be reached"); 
} 

if(NaN === false) { 
    console.log("Will not be reached"); 
} 

、これらの「falsy」の値がで使用されるかもしれませんが、交換可能に文は、彼らはすべて(特に0のセットは互いに等しい(==)ではない「場合」、」他の3人は間違っている)。より厳密な等号(====)が使用されている場合、これらはどれもfalseには等しくないので、おそらくfalseの代わりに "falsy"の分類になります。

関連する問題