Skip to content

Instantly share code, notes, and snippets.

@jfreels
Last active December 25, 2015 00:48
Show Gist options
  • Save jfreels/6889968 to your computer and use it in GitHub Desktop.
Save jfreels/6889968 to your computer and use it in GitHub Desktop.
http://learn.appendTo.com : Javascript notes
//// useful sites
// http://jsbin.com
// http://jshint.com
// http://jslint.com
// http://jsfiddle.com
// http://stackoverflow.com/questions/11246/best-resources-to-learn-javascript
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript?redirectlocale=en-US&redirectslug=JavaScript%2FA_re-introduction_to_JavaScript
//// video tutorials
// http://learn.appendto.com
// Numbers, Strings, and Loose Typing
parseInt('3.1416', 10) // 3
parseInt('3.99', 10) // 3
parseInt(3.1416, 10) // 3
parseInt(3.99, 10) // 3
parseInt('oops') // NaN
isNaN(parseInt('oops')) // true
parseFloat('3.1416') // 3.1416
+ '3.1416' // 3.1416
'foo' + 'bar' // 'foobar'
'foo' + '3.1416' // 'foo3.1416'
typeof('foo') // 'string'
typeof('3.1416') // 'string'
typeof(3.1416) // 'number'
// Comparisons 1
1 == 1 // true
2 == 'something' // false
'whiz' < 'bang' // false
'foo' >= 'bar' // true
1 == '1' // true
1 != '1' // false
1 === '1' // false
1 !== '1' // true
0 == false // true
0 === false // false
9 == 9.0 // true
9 === 9.0 // true
// If Statement
if (condition) statement
if (condition) { multiple statements }
if (condition) {
statement if true
} else {
statement if false
}
if (condition) {
statment if true
} else if {
statement if true and previous if is false
}
// Comparisons 2, Undefined and Null
undefined == null // true
undefined === null // false
// While Loops
while (condition) statement // if condition===true then while statement evaluates until the condition===false
var x = 0;
while (x<5) {
console.log(x)
x = x + 1
} // 0 1 2 3 4
// For Loops
for ([initialExpression]; [condition]; [incrementExpression]) statement
var arrrr = [ 'yo', 'ho', 'rum' ]
for (index = 0; index < arrrr.length; index++) {
console.log(index + ': ' + arrrr[index]);
} // 0: yo 1: ho 2: rum
for (var index = 0, arrrr = ['yo','ho','rum'];
index < arrrr.length;
index++) {
console.log(index + ': ' + arrrr[index]);
}
// Objects 1
var alice = {
size: 'small',
'arch-enemy' : 'the red queen',
'age' : 7.5
};
alice.size // 'small'
alice.arch-enemy // 'the red queen'
alice.age // age
// Functions 1
function functionName (parameters) {
statement
} // define function using function notation
var functionName = function(parameters) {
statement
} // define function using variable notation
functionName() // invoke function
// Objects 2
var alice = {
'age': 7.5,
'enemy': {
'name': 'Queen of Hearts',
'nationality': 'Wonderland'
}
}
alice.age // 7.5
alice.enemy // Object {name: "Queen of Hearts", nationality: "Wonderland"}
alice.enemy.name // 'Queen of Hearts'
alice.age && alice.enemy // Object {name: "Queen of Hearts", nationality: "Wonderland"}
alice.age && alice.enemy && alice.enemy.name // 'Queen of Hearts'
alice.enemy.speak = function() {
return 'off with your head!!!';
}
alice.enemy.speak() // 'off with your head!!!'
alice.enemy.speak = function() {
return 'I\'m the ' + this.name + ' ... now off with your head!!!';
}
alice.enemy.speak() // "I'm the Queen of Hearts ... now off with your head!!!"
var alice = {
'age': 7.5,
'enemy': 'Queen of Hearts'
}
for (var attr in alice) {
console.log(attr + ': ' + alice[attr])
} // age: 7.5 enemy: Queen of Hearts
// Array Objects 1
var arrrr = ['bottle','of','rum',5]
arrrr.length // 4
arrrr['length'] // 4
typeof(arrrr) // 'object'
arrrr[100] = 'test'
arrrr.length // 101
arrrr[100] // 'test'
var arrrr = ['bottle','of','rum',5]
arrrr.push('hello')
arrrr.length // 5
arrrr[4] // 'hello'
arrrr[arrrr.length-1] // 'hello'
var ary = ['foo','bar','baz']
ary.newProp = 'array\'s are objects too!!'
for (var i in ary) {
console.log(i + ': ' + ary[i])
} // foo bar baz arry's are objects too!!
for (var i=0; i<ary.length; i++) {
console.log(i + ": " + ary[i])
} // 0: foo 1: bar 2: baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment