HTML
- Import:
<script type="text/javascript" src="js/file.js"></script>- Inline:
<script type="text/javascript">alert('Hello World');</script>- Call:
<button id="start" onClick="start()" style="">Start</button>
<script type="text/javascript" src="js/file.js"></script><script type="text/javascript">alert('Hello World');</script><button id="start" onClick="start()" style="">Start</button>var name;undefinedvar name = 1;name = 1;typeof namea === b== which only checks for value equalitydocument.write('Hello World
')window.location = 'http://www.google.com'alert('Hello World')console.log('Hello World')parseInt(string, base)parseFloat(string)Math.round(num * 100) / 100NaNisNaN(value) to check for it because its not possible to check via the == operatorInfinity and -InfinityisFinite(nubmer) to check for it (returns false for NaN too)undefinednulldecodeURIdecodeURIComponentencodeURIencodeURIComponentevalunevalJavaScript objects are simply collections of name-value pairs. Similar to dictionaries or maps in other languages.
var obj = new Object();var obj = {};obj.name = "Simon";var name = obj.name;obj["name"] = "Simon";var name = obj["name"];var obj = {
name: "Carrot",
"for": "Max",
details: {
color: "orange",
size: 12
}
}function Person(first, last) {
this.first = first;
this.last = last;
}
Person.prototype.fullName = function() {
return this.first + ' ' + this.last;
}
Person.prototype.fullNameReversed = function() {
return this.last + ', ' + this.first;
}
this refers to the object before the .. new creates a new object and binds it to this. For example:
s = new Person("Simon", "Willison")
s.fullName;
obj.hasOwnProperty("prop")"prop" in obj it doesn't check the prototype chainisPrototypeOfobj.propertyIsEnumerable("prop")for ... in .. loopobj.toString() and obj.toLocaleString()Date) take the configured locale (country/language settings) into accountobj.valueOf()Describe: Prototype chain
function add(x, y) {
var total = x + y;
return total;
}or anonymous: var add = function(x, y) {
var total = x + y;
return total;
}
Functions implicitly define the array arguments which contains all parameters. These can be more or less than declared in the function header.
func.apply(null, [2, 3, 4, 5])callJavaScript objects are simply collections of name-value pairs.
var a = new Array();
a[0] = "dog";
a[1] = "cat";
a[2] = "hen";
or
var a = ["dog", "cat", "hen"];
a.lenhgtfor (var i = 0; i < a.length; i++) {
//Do something with a[i]
}
Do not use the following because it will also loop over every property of the array object:
for (var i in a) {
// Do something with a[i]
}a.toString()a.toLocaleString()a.concat(item[, itemN])a.join(sep)a.pop()a.push(item[, itemN])a.reverse()a.shift()a.slice(start, end)a.sort([cmpfn])a.splice(start, delcount[, itemN])a.unshift([item])