102-06: JavaScript
About JavaScript
- Programming language often seen in web development (among other uses)
- Compiles during execution (“Just-in-Time” computation)
- Can treat functions as variables (“First-Class Functions”) for use by other functions or operations
- Trademarked by Oracle
- Can be programmed for client-side inputs to affect the rendered output
Variables
- Use
var
to define variables and store values, like:
var a = 12;
- Variable definitions may refer to other variables, like:
‘var grandtotal = subtotal1 + subtotal2;
var subtotal1 = a + b + c;
var subtotal2 = x + y + z;`
Identifiers
- Used to store variables
- Naming restrictions: Letters & limited characters (underscore & dollar sign); case-sensitive; some command words excluded
Assignment Operator
=
- Not an “equals” operation (this is
==
in JavaScript)
- Can be used to adjust value of stored variable by referring to existing value, like:
var x = x +1;
to add 1 to the value of the “x” variable
Stored Values
- Numerals
- Text strings (; must be in quotes), like
var shave_and_a_haircut = "Two Bits";
- (Not exhaustive list of possible value types)
- Variables may exist without defined value by being declared first, like
var Whatsit
and can be assigned a value separately from declaration
- Multiple variables may be declared in one line(or several), separated by commas and each after respective assignment operators, like :`var favfood = “Pizza”, favcolor = “Purple”;
- Re-declaration (alone) does not affect stored value
- A numeral in quotes is treated as a text string, as are subsequent numerals in declaration, and will have a value of concatenated digits rather than arithmetic
Variable Interactions
- Arithmetic interactions
- Multiple text strings displayed in order, like
var sandwich = "PB" + "J"
which outputs sandwich
as "PBJ"