summaryrefslogtreecommitdiffhomepage
path: root/javascript.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'javascript.html.markdown')
-rw-r--r--javascript.html.markdown93
1 files changed, 51 insertions, 42 deletions
diff --git a/javascript.html.markdown b/javascript.html.markdown
index 7c869b28..588ea86d 100644
--- a/javascript.html.markdown
+++ b/javascript.html.markdown
@@ -39,13 +39,14 @@ doStuff()
// 1. Numbers, Strings and Operators
// JavaScript has one number type (which is a 64-bit IEEE 754 double).
-// As with Lua, don't freak out about the lack of ints: doubles have a 52-bit
-// mantissa, which is enough to store integers up to about 9✕10¹⁵ precisely.
+// Doubles have a 52-bit mantissa, which is enough to store integers
+// up to about 9✕10¹⁵ precisely.
3; // = 3
1.5; // = 1.5
-// All the basic arithmetic works as you'd expect.
+// Some basic arithmetic works as you'd expect.
1 + 1; // = 2
+0.1 + 0.2; // = 0.30000000000000004
8 - 1; // = 7
10 * 2; // = 20
35 / 5; // = 7
@@ -77,13 +78,13 @@ false;
!true; // = false
!false; // = true
-// Equality is ==
-1 == 1; // = true
-2 == 1; // = false
+// Equality is ===
+1 === 1; // = true
+2 === 1; // = false
-// Inequality is !=
-1 != 1; // = false
-2 != 1; // = true
+// Inequality is !==
+1 !== 1; // = false
+2 !== 1; // = true
// More comparisons
1 < 10; // = true
@@ -97,25 +98,31 @@ false;
// and are compared with < and >
"a" < "b"; // = true
-// Type coercion is performed for comparisons...
+// Type coercion is performed for comparisons with double equals...
"5" == 5; // = true
+null == undefined; // = true
// ...unless you use ===
"5" === 5; // = false
+null === undefined; // = false
-// You can access characters in a string with charAt
+// ...which can result in some weird behaviour...
+13 + !0; // 14
+"13" + !0; // '13true'
+
+// You can access characters in a string with `charAt`
"This is a string".charAt(0); // = 'T'
-// ...or use substring to get larger pieces
+// ...or use `substring` to get larger pieces.
"Hello world".substring(0, 5); // = "Hello"
-// length is a property, so don't use ()
+// `length` is a property, so don't use ().
"Hello".length; // = 5
-// There's also null and undefined
-null; // used to indicate a deliberate non-value
+// There's also `null` and `undefined`.
+null; // used to indicate a deliberate non-value
undefined; // used to indicate a value is not currently present (although
- // undefined is actually a value itself)
+ // `undefined` is actually a value itself)
// false, null, undefined, NaN, 0 and "" are falsy; everything else is truthy.
// Note that 0 is falsy and "0" is truthy, even though 0 == "0".
@@ -123,8 +130,9 @@ undefined; // used to indicate a value is not currently present (although
///////////////////////////////////
// 2. Variables, Arrays and Objects
-// Variables are declared with the var keyword. JavaScript is dynamically typed,
-// so you don't need to specify type. Assignment uses a single = character.
+// Variables are declared with the `var` keyword. JavaScript is dynamically
+// typed, so you don't need to specify type. Assignment uses a single `=`
+// character.
var someVar = 5;
// if you leave the var keyword off, you won't get an error...
@@ -158,7 +166,7 @@ myArray.length; // = 4
// Add/Modify at specific index
myArray[3] = "Hello";
-// JavaScript's objects are equivalent to 'dictionaries' or 'maps' in other
+// JavaScript's objects are equivalent to "dictionaries" or "maps" in other
// languages: an unordered collection of key-value pairs.
var myObj = {key1: "Hello", key2: "World"};
@@ -183,7 +191,7 @@ myObj.myFourthKey; // = undefined
// The syntax for this section is almost identical to Java's.
-// The if structure works as you'd expect.
+// The `if` structure works as you'd expect.
var count = 1;
if (count == 3){
// evaluated if count is 3
@@ -193,18 +201,18 @@ if (count == 3){
// evaluated if it's not either 3 or 4
}
-// As does while.
+// As does `while`.
while (true){
// An infinite loop!
}
// Do-while loops are like while loops, except they always run at least once.
-var input
+var input;
do {
input = getInput();
} while (!isValid(input))
-// the for loop is the same as C and Java:
+// The `for` loop is the same as C and Java:
// initialisation; continue condition; iteration.
for (var i = 0; i < 5; i++){
// will run 5 times
@@ -222,7 +230,7 @@ if (colour == "red" || colour == "blue"){
var name = otherName || "default";
-// switch statement checks for equality with ===
+// The `switch` statement checks for equality with `===`.
// use 'break' after each case
// or the cases after the correct one will be executed too.
grade = 'B';
@@ -245,14 +253,14 @@ switch (grade) {
///////////////////////////////////
// 4. Functions, Scope and Closures
-// JavaScript functions are declared with the function keyword.
+// JavaScript functions are declared with the `function` keyword.
function myFunction(thing){
return thing.toUpperCase();
}
myFunction("foo"); // = "FOO"
// Note that the value to be returned must start on the same line as the
-// 'return' keyword, otherwise you'll always return 'undefined' due to
+// `return` keyword, otherwise you'll always return `undefined` due to
// automatic semicolon insertion. Watch out for this when using Allman style.
function myFunction()
{
@@ -291,8 +299,8 @@ i; // = 5 - not undefined as you'd expect in a block-scoped language
// scope.
(function(){
var temporary = 5;
- // We can access the global scope by assiging to the 'global object', which
- // in a web browser is always 'window'. The global object may have a
+ // We can access the global scope by assiging to the "global object", which
+ // in a web browser is always `window`. The global object may have a
// different name in non-browser environments such as Node.js.
window.permanent = 10;
})();
@@ -305,7 +313,7 @@ permanent; // = 10
function sayHelloInFiveSeconds(name){
var prompt = "Hello, " + name + "!";
// Inner functions are put in the local scope by default, as if they were
- // declared with 'var'.
+ // declared with `var`.
function inner(){
alert(prompt);
}
@@ -313,7 +321,7 @@ function sayHelloInFiveSeconds(name){
// setTimeout is asynchronous, so the sayHelloInFiveSeconds function will
// exit immediately, and setTimeout will call inner afterwards. However,
// because inner is "closed over" sayHelloInFiveSeconds, inner still has
- // access to the 'prompt' variable when it is finally called.
+ // access to the `prompt` variable when it is finally called.
}
sayHelloInFiveSeconds("Adam"); // will open a popup with "Hello, Adam!" in 5s
@@ -329,7 +337,7 @@ var myObj = {
myObj.myFunc(); // = "Hello world!"
// When functions attached to an object are called, they can access the object
-// they're attached to using the this keyword.
+// they're attached to using the `this` keyword.
myObj = {
myString: "Hello world!",
myFunc: function(){
@@ -345,7 +353,7 @@ var myFunc = myObj.myFunc;
myFunc(); // = undefined
// Inversely, a function can be assigned to the object and gain access to it
-// through this, even if it wasn't attached when it was defined.
+// through `this`, even if it wasn't attached when it was defined.
var myOtherFunc = function(){
return this.myString.toUpperCase();
}
@@ -353,37 +361,38 @@ myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "HELLO WORLD!"
// We can also specify a context for a function to execute in when we invoke it
-// using 'call' or 'apply'.
+// using `call` or `apply`.
var anotherFunc = function(s){
return this.myString + s;
}
anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!"
-// The 'apply' function is nearly identical, but takes an array for an argument list.
+// The `apply` function is nearly identical, but takes an array for an argument
+// list.
anotherFunc.apply(myObj, [" And Hello Sun!"]); // = "Hello World! And Hello Sun!"
-// This is useful when working with a function that accepts a sequence of arguments
-// and you want to pass an array.
+// This is useful when working with a function that accepts a sequence of
+// arguments and you want to pass an array.
Math.min(42, 6, 27); // = 6
Math.min([42, 6, 27]); // = NaN (uh-oh!)
Math.min.apply(Math, [42, 6, 27]); // = 6
-// But, 'call' and 'apply' are only temporary. When we want it to stick, we can use
-// bind.
+// But, `call` and `apply` are only temporary. When we want it to stick, we can
+// use `bind`.
var boundFunc = anotherFunc.bind(myObj);
boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!"
-// Bind can also be used to partially apply (curry) a function.
+// `bind` can also be used to partially apply (curry) a function.
var product = function(a, b){ return a * b; }
var doubler = product.bind(this, 2);
doubler(8); // = 16
-// When you call a function with the new keyword, a new object is created, and
+// When you call a function with the `new` keyword, a new object is created, and
// made available to the function via the this keyword. Functions designed to be
// called like that are called constructors.
@@ -398,7 +407,7 @@ myNewObj.myNumber; // = 5
// look at its prototype.
// Some JS implementations let you access an object's prototype on the magic
-// property __proto__. While this is useful for explaining prototypes it's not
+// property `__proto__`. While this is useful for explaining prototypes it's not
// part of the standard; we'll get to standard ways of using prototypes later.
var myObj = {
myString: "Hello world!"
@@ -429,7 +438,7 @@ myObj.myBoolean; // = true
myPrototype.meaningOfLife = 43;
myObj.meaningOfLife; // = 43
-// We mentioned that __proto__ was non-standard, and there's no standard way to
+// We mentioned that `__proto__` was non-standard, and there's no standard way to
// change the prototype of an existing object. However, there are two ways to
// create a new object with a given prototype.