COMP 1231 Web Programming Ben Blanc Week 2
COMP 1231 Web Programming Ben Blanc
Week 2 Java. Script Basics
My NEW Schedule
JS Identifier
JS Basics
Variable Scopes • Keyword ‘var’ is used to create variables. – var nameofvar=value; • Variables declared outside of a function are GLOBAL • Omitting the ‘var’ keyword and assigning a variable a value makes the variable scope GLOBAL – nameofvar=value;
Primitive Data Types • String • Number – Integer – Floating point/decimal • Boolean
Strings • String variable is any value enclosed in either single or double quotes • Concat (combine) strings together with the “+” symbol – Ensure concatenation symbol is not the last character in a JS statement • Default value for string is undefined
JS Console • Main methods – console. log() – console. error() – console. warn() – console. info() – console. debug()
Window Object Review • Common window methods – window. open() • Returns window object reference to new window or null – window. close() – window. alert() – window. prompt() • Returns string value or null if cancelled – window. confirm() • Returns boolean true if “OK”, false otherwise
Document object Review • Common methods – Write() – get. Element. By. Id() • Returns HTML element object – HTML_OBJECT. inner. HTML to write to object
Concatenation and Assignment
Numbers • To code a number – var num 1=123; //will be interpreted as integer – var num 2=12. 3 //will be interpreted as float • To typecast (convert) string to number – parse. Int() – parse. Float() • To check if value is number – is. Na. N() //does typecasting on check but doesn’t typecast variable itself
Arithmetic (Number) Operators • • var x=6; var y=3; var z=0; //can also be var x=6, y=3, z=0; • z=x+y, z=x-y, z=x%y • Works as expected because all variables are numbers
Common Arithmetic Operators
Arithmetic Assignment • Shorthand version • Updates value of one variable by value of another variable • var x=6, y=3; • x+=y x-=y x%=y
Why Can’t It Understand? ! • Is a number enclosed in quotes a number or a string? – Technical Answer: string – Situational answer: depends
Date Object • var identifier=new Date(arguments); • Arguments – new Date(number) • Integer in milliseconds – new Date(string) • Month and year min • DOW, Mon, Day, Year, Time (Hour: Min: Sec: Ms) – New Date(7 number args) • Year, Mon, Day min • Year, Mon, Day, Hour, Min, Sec, Mill – Mon is 0 -11
Date Object • DATE. to. String() – Full date • Date. to. Time. String() – Just time portion • Date. set. Time(number) – Adds number of milliseconds to Jan 1, 1970
Date Object • DATE. get. Month() – Also can be Minutes, Day, Milliseconds, Hours – Returns requested data • DATE. set. Month() – Same as above – Sets requested data • More functions – http: //www. w 3 schools. com/jsref_obj_date. asp
Dealing With Errors • Syntax Error – Missing semi-colon – Quotes mis-match • Runtime Error – Accessing undefined var – Invalid identifier • Logic Error – Program runs but has unexpected results
- Slides: 21