summaryrefslogtreecommitdiffhomepage
path: root/javascript.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'javascript.html.markdown')
-rw-r--r--javascript.html.markdown53
1 files changed, 45 insertions, 8 deletions
diff --git a/javascript.html.markdown b/javascript.html.markdown
index 7fb7ba55..76017c17 100644
--- a/javascript.html.markdown
+++ b/javascript.html.markdown
@@ -2,10 +2,11 @@
language: javascript
contributors:
- ["Adam Brenecki", "http://adam.brenecki.id.au"]
+ - ["Ariel Krakowski", "http://www.learneroo.com"]
filename: javascript.js
---
-Javascript was created by Netscape's Brendan Eich in 1995. It was originally
+JavaScript was created by Netscape's Brendan Eich in 1995. It was originally
intended as a simpler scripting language for websites, complimenting the use of
Java for more complex web applications, but its tight integration with Web pages
and built-in support in browsers has caused it to become far more common than
@@ -37,7 +38,7 @@ doStuff()
///////////////////////////////////
// 1. Numbers, Strings and Operators
-// Javascript has one number type (which is a 64-bit IEEE 754 double).
+// 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.
3; // = 3
@@ -103,7 +104,13 @@ false;
"5" === 5; // = false
// You can access characters in a string with charAt
-"This is a string".charAt(0);
+"This is a string".charAt(0); // = 'T'
+
+// ...or use substring to get larger pieces
+"Hello world".substring(0, 5); // = "Hello"
+
+// 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
@@ -116,7 +123,7 @@ 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,
+// 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;
@@ -148,6 +155,9 @@ myArray[1]; // = 45
myArray.push("World");
myArray.length; // = 4
+// Add/Modify at specific index
+myArray[3] = "Hello";
+
// 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"};
@@ -171,18 +181,20 @@ myObj.myFourthKey; // = undefined
///////////////////////////////////
// 3. Logic and Control Structures
+// The syntax for this section is almost identical to Java's.
+
// The if structure works as you'd expect.
var count = 1;
if (count == 3){
// evaluated if count is 3
-} else if (count == 4) {
+} else if (count == 4){
// evaluated if count is 4
} else {
// evaluated if it's not either 3 or 4
}
// As does while.
-while (true) {
+while (true){
// An infinite loop!
}
@@ -209,6 +221,27 @@ if (colour == "red" || colour == "blue"){
// && and || "short circuit", which is useful for setting default values.
var name = otherName || "default";
+
+// switch statement checks for equality with ===
+// use 'break' after each case
+// or the cases after the correct one will be executed too.
+grade = 'B';
+switch (grade) {
+ case 'A':
+ console.log("Great job");
+ break;
+ case 'B':
+ console.log("OK job");
+ break;
+ case 'C':
+ console.log("You can do better");
+ break;
+ default:
+ console.log("Oy vey");
+ break;
+}
+
+
///////////////////////////////////
// 4. Functions, Scope and Closures
@@ -327,7 +360,7 @@ var anotherFunc = function(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!"
@@ -477,9 +510,13 @@ more about how to use JavaScript in web pages, start by learning about the
[Document Object
Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)
-[Javascript Garden](http://bonsaiden.github.io/JavaScript-Garden/) is an in-depth
+[Learn Javascript by Example and with Challenges](http://www.learneroo.com/modules/64/nodes/350) is a variant of this reference with built-in challenges.
+
+[JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/) is an in-depth
guide of all the counter-intuitive parts of the language.
+[JavaScript: The Definitive Guide](http://www.amazon.com/gp/product/0596805527/) is a classic guide / reference book.
+
In addition to direct contributors to this article, some content is adapted
from Louie Dinh's Python tutorial on this site, and the [JS
Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)