Incorporating JS into a web page
The main point of JS is to offer dynamic functionality to what previously were static web pages.
Where you input the JS on a webpage, becomes the same place it output. Thus where you place JS is important!
Comments
// This is a commment
anumber = 42 //Assigns 42 to anumber
/* This is a muti-line comment */
Semicolons
It is optional to add a ; to the end of every line
Inserting JS
<script> </script>
<script src="myscript.js"></script>
Syntax
JS is a case-sensitive language - myvariable does not equal MyVariable
Variable Naming Conventions
Can name variables starting with a-z, A-Z, $ or _
Escape Characters
- \' = single quote
- \" = double quote
- \ = backslash
- \b = backspace
- \f = form feed
- \n = new line
- \r = carriage return
- \t = tab
Casting Functions
- Boolean() = can be true or false
- Number() = change to number
- String() = change to string
Variables can change from string to integer and other automatically without calling it
Operators
- +
- -
- *
- /
- % = Modulus (remainder after division)
- ++ = Increment
- -- = Decrement
There is a long list of arithmetic functions such as Math.cos(a)
= returns the cosine of a
Assignment operators enable an operator with equal to do more such as:
a = 20; a += 6; Results in 26
Comparison operators compare values such as:
1==1
1>2
Ternary Operator
If this then do that thing otherwise do another thing:
expression ? do this : do that
ex. AmPm = Time < 12 ? 'AM' : 'PM'
with Keyword
string = "the dog barks"
with (string)
{
document.write("the string's length is " + length)
document.write("<br />UpperCase: " + toUpperCase())
}
...
The string's length is 13 characters
UpperCase: THE DOG BARKS