<script>
tagstype
attribute, but HTML5 assumes JavaScriptscript
tagssrc
attribute<script>
JS GOES HERE
</script>
<script src="file_name.js"></script>
noscript
Tag¶noscript
tagnoscript
is only displayed if running JavaScript is not possiblevar a = 1
var _a = 1
var $a = 1
//var 1a = 1
var a = 5
if(true)
{
var b = 6
}
a + b
var c = 6
function f() {
var d = 11
}
c + d
var e = 5
if(true)
{
let g = 6
}
e + g
4 + 1
4 - 1
4 * 1
4 / -2
4 % -2
4/0
Infinity / Infinity
Math.sqrt(-1)
0/0
var a = 1
a += 1
a
var b = 20
b++
b
"abc"[0]
"abc" + "abc"
'Someone said "this" '
charAt(i)
allows you to index using a method rather than []
concat(s1,s2...)
allows multiple strings to be concatenated in one callindexOf(string)
/lastIndexOf(string)
finds the first or last occurrence of the argument in the stringsplit(sep)
returns an array, the result of splitting the string on the separator passed inlength
is a property that holds the number of characters in a string"abc".charAt(0)
"abc".concat("def","ghi",'jkl')
"abba".indexOf('b')
"abba".lastIndexOf('b')
"a,b,c,d".split(",")
var x = "a,,b,c,,d".split(",,")
x
"abc".length
(1 > 0 ) && (1 < 10)
'1' == 1
'1' === 1
'1' != 1
'1' !== 1
var undeclared
console.log(undeclared === undefined)
console.log(undeclared === null)
console.log(undeclared == null)
console.log(null == false)
console.log("abc"[200])
Array()
withvar arr = [1,2,3,4,5,6]
console.log(arr)
console.log(arr[0])
var arr2 = Array(10)
console.log(arr2)
console.log(arr2[0])
var arr3 = Array(10,9,8,7,6)
console.log(arr3)
console.log(arr3[0])
console.log(arr3[-1])
concat(a1,a2,a3)
Appends several arrays together into one arrayjoin(string)
Returns a string, with each element joined by a stringpop/push(el)
Remove or add an element at the end of the arrayshift/unshift(el)
Remove or add an element at the front of the arrayreverse()
Returns array in reverse ordersort(function)
Returns the array, sorted by a functionvar my_array = Array(1,2,3,4)
my_array.concat([1,2,3,4],[1,2,3,4])
my_array.join(",")
my_array.join("...")
var my_array2 = Array(1,2,3,4,5)
my_array2.pop()
console.log(my_array2)
my_array2.push("Elephant")
console.log(my_array2)
var my_array3 = Array(10,9,8,7,6,5)
console.log(my_array3.shift())
console.log(my_array3)
my_array3.unshift("T-minus")
console.log(my_array3)
2 - '20'
5 + Number('1')
5 + '1'
'1' + 2
'5' * 20
'5' * '5'
't' * 5
't' / null
null == 0
'8' / null
if (condition) {
doSomething
}
if (condition1){
}
else if(condition2){
}
else if(condition3){
}
else{
}
var x = '0'
if(x < 0){
console.log("Negative");
}
//
/*
Note the triple equals
*/
else if(x === 0){
console.log("Zero");
}
else{
console.log("Positive");
}
switch(toTest){
case 1:
case 2:
doSomething
break
case "A":
case "B":
somethingElse
break
case "D":
other
break
default:
final
break
}
switch('0'){
case -1:
console.log("Negative")
break
case 0:
//case '0':
console.log("Zero")
break
default:
console.log("Positive")
break
}
for(var i = 0; i < 10; i++){
}
var i = 0
while(i < 10){
i++
}
for(let z = 0; z < 10; z++){
console.log(z * z)
}
console.log(z)
var q = 1
while(q < 10){
q++
}
for in
loop will loop over an objects keysfor of
loop is new, and iterates directly over the values of an objectlet to_loop = ['a','b','c',1,2,3]
for (i in to_loop){
console.log(i, to_loop[i])
}
for (j of to_loop){
console.log(j)
}
function name(param1, param2, ...){
}
square(10)
function square(x){
return x*x
}
function counter(){
var count = 0;
return function(){
count++
return count
}
}
var c = counter()
c() + 1