This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var people = ['Bob', 'Sally', 'Sara', 'Frank']; | |
console.log(people.length); // 4 | |
var i; | |
var len = people.length; | |
for (i = 0; i < len; i += 1) { | |
console.log(people[i]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = 100, | |
b = 0; | |
function change() { | |
var b = a + 5; | |
} | |
change(); | |
alert(b); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person = { | |
first_name: "Robert", | |
last_name: "Barker", | |
nick_name: "Bob", | |
full_name: undefined, | |
name_helper: function (short) { | |
var that = this; // the important part | |
var shorter_name = function () { | |
// this to that | |
that.full_name = that.nick_name + " " + that.last_name; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person = { | |
first_name: "Robert", | |
last_name: "Barker", | |
nick_name: "Bob", | |
full_name: undefined, | |
name_helper: function (short) { | |
var shorter_name = function () { | |
this.full_name = this.nick_name + " " + this.last_name; | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var counter = { | |
value: 0, | |
increment: function () { | |
this.value += 1; | |
}, | |
reset: function () { | |
this.value = 0; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add | |
var add = function (a, b) { | |
return a + b; | |
}; | |
// or | |
function add (a, b) { | |
return a + b; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person = { | |
"name": "Bob Barker" | |
}; | |
person.name = "Batman"; | |
//or | |
person["name"] = "Batman"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var student = { | |
"name": "Bob Barker", | |
"age": 100 | |
}; | |
console.log(student.name); | |
console.log(student.age); | |
console.log(student["name"]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(x, y) { | |
// antipattern: implied global | |
result = x + y; | |
return result; | |
} | |
function sum(x, y) { | |
var result = x + y; | |
return result; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.writeln('hello world!'); // using the document object to directly write to the page | |
alert('hello world!'); // issuing an alert dialog box. | |
console.log('hello world!'); // writing to the browser console. |