Class 3 Notes for Web Scripting

Learning Objectives:

You will be able identify the data types available to Javascript

You will be able identify the arithmetic operators available to Javascript

 

Homework:

Read: Chapter 2

Create: Simple Calculator and Link to Homework page!

Review: What is a Web Developer (Job requirements)

Review: Variables & Datatypes on W3schools

 

Topics Covered:

Best Practices (I don't agree with all of these)

The display difference between document.write(variable) or getElementById("myid").innerHTML = variable

Data Type:

Numerical (123..), text (string) and boolean (True - False)

Variables:

A variable is a placeholder or container for use within the program. You should but you don't have to declare your variables.

var myvariable

Syntax for Variables

  • Variable names are case sensitive
  • They must begin with a letter or the underscore
  • The variable must not use a reserved word
  • abstract
  • as
  • boolean
  • break
  • byte
  • case
  • catch
  • char
  • class
  • continue
  • const
  • debugger
  • default
  • delete
  • do
  • double
  • else
  • enum
  • export
  • extends
  • false
  • final
  • finally
  • float
  • for
  • function
  • goto
  • if
  • implements
  • import
  • in
  • instanceof
  • int
  • interface
  • is
  • long
  • namespace
  • native
  • new
  • null
  • package
  • private
  • protected
  • public
  • return
  • short
  • static
  • super
  • switch
  • synchronized
  • this
  • throw
  • throws
  • transient
  • true
  • try
  • typeof
  • use
  • var
  • void
  • volatile
  • while
  • with

Global vs Local variables: When you declare a variable within a function, the variable can only be accessed within that function.

Arithmetic Operators

Operator Description Example Result
+ Addition x=4
y=2
x+y
- Subtraction x=5
y=3
x-y
2
* Multiplication x=5
y=6
x*y
30
/ Division 20/5
5/2
4
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=8
x++
x=9
-- Decrement x=9
x--
x=8

Assignment Operators

Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y

Comparison Operators

Operator Description Example
== is equal to 6==7 returns false
===

is equal to

The difference is it checks both value and type!

x=5
y="5"

x==y returns true
x===y returns false

!= is not equal 9!=8 returns true
> is greater than 2>7 returns false
< is less than 7<9 returns true
>= is greater than or equal to 6>=7 returns false
<= is less than or equal to 1<=6 returns true

VIP:

+ is used for concatination and for addition. You have to pay attention to how the data is classified (number or string). If you need to make sure a variable is a number, use Number() methnod.

Example: X =A + B can be written as X=Number(A) + Number(B)

 

Arrays:

Varialbes on steroids. You can have single or multi-demansional arrays

var arrayName = new Array();

var airplane = new Array();
airplane[0] = "747";
airplane[1] = "787";
airplane[2] = "777";

or

var arrayName = ["747", "787", "777", "737"];

var airplane= ["747", "787", "777", "737"];

 

 

HTML Valid