Added a bit of example code and resources.

This commit is contained in:
Ariel 2014-03-10 17:23:49 -04:00
parent 38232deaeb
commit 0590851a64

View File

@ -2,6 +2,7 @@
language: javascript language: javascript
contributors: contributors:
- ["Adam Brenecki", "http://adam.brenecki.id.au"] - ["Adam Brenecki", "http://adam.brenecki.id.au"]
- ["Ariel Krakowski", "http://www.learneroo.com"]
filename: javascript.js filename: javascript.js
--- ---
@ -103,7 +104,13 @@ false;
"5" === 5; // = false "5" === 5; // = false
// You can access characters in a string with charAt // 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 // There's also null and undefined
null; // used to indicate a deliberate non-value null; // used to indicate a deliberate non-value
@ -148,6 +155,9 @@ myArray[1]; // = 45
myArray.push("World"); myArray.push("World");
myArray.length; // = 4 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. // languages: an unordered collection of key-value pairs.
var myObj = {key1: "Hello", key2: "World"}; var myObj = {key1: "Hello", key2: "World"};
@ -171,6 +181,8 @@ myObj.myFourthKey; // = undefined
/////////////////////////////////// ///////////////////////////////////
// 3. Logic and Control Structures // 3. Logic and Control Structures
// 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; var count = 1;
if (count == 3){ if (count == 3){
@ -209,6 +221,27 @@ if (colour == "red" || colour == "blue"){
// && and || "short circuit", which is useful for setting default values. // && and || "short circuit", which is useful for setting default values.
var name = otherName || "default"; 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 // 4. Functions, Scope and Closures
@ -477,9 +510,13 @@ more about how to use JavaScript in web pages, start by learning about the
[Document Object [Document Object
Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core) Model](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)
[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 [JavaScript Garden](http://bonsaiden.github.io/JavaScript-Garden/) is an in-depth
guide of all the counter-intuitive parts of the language. 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 In addition to direct contributors to this article, some content is adapted
from Louie Dinh's Python tutorial on this site, and the [JS 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) Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)