From 094a2f6ee9ea69ad2e18aec6abff08910b66aed5 Mon Sep 17 00:00:00 2001 From: Marco Rogers Date: Fri, 12 Aug 2011 01:19:16 -0700 Subject: [PATCH 1/2] [docs] what is the arguments object --- .../what-is-the-arguments-object/article.md | 49 +++++++++++++++++++ .../metadata.json | 7 +++ 2 files changed, 56 insertions(+) create mode 100644 articles/basic/what-is-the-arguments-object/article.md create mode 100644 articles/basic/what-is-the-arguments-object/metadata.json diff --git a/articles/basic/what-is-the-arguments-object/article.md b/articles/basic/what-is-the-arguments-object/article.md new file mode 100644 index 0000000..be43f75 --- /dev/null +++ b/articles/basic/what-is-the-arguments-object/article.md @@ -0,0 +1,49 @@ +# arguments + +The arguments object is a special construct available inside all +function calls. It represents the list of arguments that were passed +in when invoking the function. Since javascript allows functions to be +called with any number args, we need a way to dynamically discover and +access them. + +The arguments object is an array-like object. It has a length +property that corresponds to the number of arguments passed into the +function. You can access these values by indexing into the array, +e.g. arguments[0] is the first argument. The only other standard +property of arguments is callee. This always refers to the function +currently being executed. Here's an example that illustrates the +properties of arguments. + + var myfunc = function(one) { + arguments.callee === myfunc; + arguments[0] === one; + arguments[1] === 2; + arguments.length === 3; + } + + myfunc(1, 2, 3); + +This construct is very useful and gives javascript functions a lot of +flexibility. But there is an important gotcha. The arguments object +behaves like an array, but it is not an actual array. It does not have +Array in it's prototype chain and it does not respond to any array +methods, e.g. arguments.sort() raises a TypeError. Instead you need to +copy the values into a true array first. Since a normal for loop +works, this is pretty easy. + + var args = []; + for(var i = 0; i < arguments.length; i++) { + args.push(arguments[i]); + } + +In certain cases you can still treat arguments as an array. You can +use arguments in dynamic function invocations using apply, +e.g. myfunc.apply(obj, arguments). And most native Array methods will +also accept arguments when dynamically invoked using call or apply, +e.g. Array.prototype.concat.call([1,2,3], arguments). This technique +also suggests another way to convert arguments into a true array using +the Array#slice method. + + var args = Array.prototype.slice.call(arguments); + + diff --git a/articles/basic/what-is-the-arguments-object/metadata.json b/articles/basic/what-is-the-arguments-object/metadata.json new file mode 100644 index 0000000..822d2ff --- /dev/null +++ b/articles/basic/what-is-the-arguments-object/metadata.json @@ -0,0 +1,7 @@ +{ + "author": "polotek", + "date": "today 11:00 pm", + "tags": ["truthy", "falsy", "types", "coercion"], + "title": "What is the arguments object?", + "difficulty":4 +} From bdd42ec99fcd3a461714504bb21c2697305ed18e Mon Sep 17 00:00:00 2001 From: Marco Rogers Date: Fri, 12 Aug 2011 01:19:41 -0700 Subject: [PATCH 2/2] [docs] what are truthy and falsy values --- .../article.md | 63 +++++++++++++++++++ .../falsy_values.js | 6 ++ .../metadata.json | 7 +++ .../operators.js | 20 ++++++ .../truthy_falsy.js | 9 +++ .../weak_typing.js | 2 + 6 files changed, 107 insertions(+) create mode 100644 articles/basic/what-are-truthy-and-falsy-values/article.md create mode 100644 articles/basic/what-are-truthy-and-falsy-values/falsy_values.js create mode 100644 articles/basic/what-are-truthy-and-falsy-values/metadata.json create mode 100644 articles/basic/what-are-truthy-and-falsy-values/operators.js create mode 100644 articles/basic/what-are-truthy-and-falsy-values/truthy_falsy.js create mode 100644 articles/basic/what-are-truthy-and-falsy-values/weak_typing.js diff --git a/articles/basic/what-are-truthy-and-falsy-values/article.md b/articles/basic/what-are-truthy-and-falsy-values/article.md new file mode 100644 index 0000000..0063f83 --- /dev/null +++ b/articles/basic/what-are-truthy-and-falsy-values/article.md @@ -0,0 +1,63 @@ +# Truthy and Falsy values + +Javascript is weakly typed language. That means different types can be +used in operations and the language will try to convert the types +until the operation makes sense. + + console.log("1" > 0); // true, "1" converted to number + console.log(1 + "1"); // 11, 1 converted to string + +Type conversion also applys when values are used in unary boolean +operations, most notably if statements. If a value converts to the +boolean true, then it is said to be "truthy". If it converts to false +it is "falsy". + + var myval = "value"; + if(myval) { + console.log("This value is truthy"); + } + + myval = 0; + if(!myval) { + console.log("This value is falsy"); + } + +Since most values in javascript are truthy, e.g. objects, arrays, most +numbers and strings, it's easier to identify all of the falsy +values. These are: + + false // obviously + 0 // The only falsy number + "" // the empty string + null + undefined + NaN + +Note that all objects and arrays are truthy, even empty ones. + +Truthiness and Falsiness also come into play with logical +operators. When using logical AND/OR, the values will be converted +based on truthiness or falsyness and then the expression will resolve +to the last truthy value. Short circuit rules apply. Here's an +extended example. + + var first = "truthy" + , second = "also truthy"; + + var myvalue = first && second; + console.log(myvalue); // "also truthy" + + first = null; + second = "truthy"; + + myvalue = first || second; + console.log(myvalue); // "truthy" + + myvalue2 = second || first; + console.log(myvalue2); // "truthy" + + var truthy = "truthy" + , falsy = 0; + + myvalue = truthy ? true : false; + myvalue = falsy ? true : false; diff --git a/articles/basic/what-are-truthy-and-falsy-values/falsy_values.js b/articles/basic/what-are-truthy-and-falsy-values/falsy_values.js new file mode 100644 index 0000000..8aec4ce --- /dev/null +++ b/articles/basic/what-are-truthy-and-falsy-values/falsy_values.js @@ -0,0 +1,6 @@ +false +0 // The only falsy number +"" // the empty string +null +undefined +NaN diff --git a/articles/basic/what-are-truthy-and-falsy-values/metadata.json b/articles/basic/what-are-truthy-and-falsy-values/metadata.json new file mode 100644 index 0000000..8b8faef --- /dev/null +++ b/articles/basic/what-are-truthy-and-falsy-values/metadata.json @@ -0,0 +1,7 @@ +{ + "author": "polotek", + "date": "today 11:00 pm", + "tags": ["truthy", "falsy", "types", "coercion"], + "title": "What are "truthy" and "falsy" values?", + "difficulty":4 +} diff --git a/articles/basic/what-are-truthy-and-falsy-values/operators.js b/articles/basic/what-are-truthy-and-falsy-values/operators.js new file mode 100644 index 0000000..95f4728 --- /dev/null +++ b/articles/basic/what-are-truthy-and-falsy-values/operators.js @@ -0,0 +1,20 @@ +var first = "truthy" + , second = "also truthy"; + +var myvalue = first && second; +console.log(myvalue); // "also truthy" + +first = null; +second = "truthy"; + +myvalue = first || second; +console.log(myvalue); // "truthy" + +myvalue2 = second || first; +console.log(myvalue2); // "truthy" + +var truthy = "truthy" + , falsy = 0; + +myvalue = truthy ? true : false; +myvalue = falsy ? true : false; diff --git a/articles/basic/what-are-truthy-and-falsy-values/truthy_falsy.js b/articles/basic/what-are-truthy-and-falsy-values/truthy_falsy.js new file mode 100644 index 0000000..f705e64 --- /dev/null +++ b/articles/basic/what-are-truthy-and-falsy-values/truthy_falsy.js @@ -0,0 +1,9 @@ +var myval = "value"; +if(myval) { + console.log("This value is truthy"); +} + +myval = 0; +if(!myval) { + console.log("This value is falsy"); +} diff --git a/articles/basic/what-are-truthy-and-falsy-values/weak_typing.js b/articles/basic/what-are-truthy-and-falsy-values/weak_typing.js new file mode 100644 index 0000000..37d081c --- /dev/null +++ b/articles/basic/what-are-truthy-and-falsy-values/weak_typing.js @@ -0,0 +1,2 @@ +console.log("1" > 0); // true, "1" converted to number +console.log(1 + "1"); // 11, 1 converted to string