javascript
javascript
JS Tutorial
웹프로그래밍
2016년 1학기
충남대학교 컴퓨터공학과
목차
JS Introduction
JS Where To, Output, Syntax
JS Statements and Comments
JS Variables & Operators
JS Data Types: Numbers, Strings, Arrays, and more
JS Functions
JS Objects
JS Scope
JS Events
JS Control Statements
2
JavaScript Introduction
3
JavaScript Introduction (cont’d)
4
JavaScript Where To
5
JavaScript Where To (cont’d)
6
JavaScript Output
7
JavaScript Syntax
JavaScript Statements
Composed of : Values, Operators, Expressions, Keywords, and
Comments
JavaScript Values
Two types of values : Fixed values and variable values
Fixed values are called literals.
Variable values are called variables.
8
JavaScript Syntax (cont’d)
JavaScript Literals
Numbers are written with or without decimals. Try it!
Strings are text, written within double or single quotes. Try it
Expression can also represent fixed values. Try it!
9
JavaScript Syntax (cont’d)
JavaScript Operators
Assignment operator (=) Try it!
Arithmetic operators ( + - * /) to compute values. Try it!
JavaScript Keywords
Used to identify actions to be performed.
The var keyword tells the browser to create a new variable.
JavaScript Comments
Single line comment : // Try it!
Multi-line Comment : /* and end with */
10
JavaScript Syntax (cont’d)
11
JavaScript Statements
JavaScript Programs
Contain many JavaScript statements.
The statements are executed, one by one, in the same order as they
are written. Try it!
12
JavaScript Statements (cont’d)
Semicolons ;
Separate JavaScript statements.
Add a semicolon at the end of each executable statement. Try it!
Multiple statements on one line are allowed. Try it!
13
JavaScript Statements (cont’d)
JavaScript Keywords
14
JavaScript Comments
Multi-line Comments
Multi-line comments start with /* and end with */. Try it
Any text between /* and */ will be ignored by JavaScript.
15
JavaScript Variables
JavaScript Identifiers
All JavaScript variables must be identified with unique names.
Unique names are called identifiers
The general rules for constructing names for variables (unique
identifiers) are.
Names can contain letters, digits, underscores, and dollar signs.
Names must begin with a letter.
Names can also begin with $ and _ .
Names are case sensitive (y and Y are different variables).
Reserved words (like JavaScript keywords) cannot be used as names.
16
JavaScript Variables (cont’d)
carname = “Volvo”;
You can also assign a value to the variable when you declare it.
17
JavaScript Variables (cont’d)
18
JavaScript Operators
Try it!
Try it!
Try it!
Try it!
Try it!
Try it!
Try it!
19
JavaScript Operators (cont’d)
Try it!
Try it!
Try it!
Try it!
Try it!
Try it!
20
JavaScript Operators (cont’d)
21
JavaScript Data Types
22
JavaScript Data Types (cont’d)
JavaScript Booleans
Only have two values : true or false
23
JavaScript Data Types (cont’d)
24
JavaScript Data Types (cont’d)
25
JavaScript Functions
JavaScript Function is Try it!
A block of code designed to perform a particular task.
Executed when “something” invokes it(calls it).
Function Invocation
The code inside the function will execute when “something” invokes
the function.
When an event occurs (when a user clicks a button)
When it is invoked (called) from JavaScript code
Automatically (self invoked)
26
JavaScript Functions (cont’d)
27
JavaScript Objects
28
JavaScript Objects (cont’d)
JavaScript Objects
Objects are variables. Objects can contain many values. Try it!
The values are written as name:value pairs
Object Properties
name:values pairs are called properties.
29
JavaScript Objects (cont’d)
30
JavaScript Scope
JavaScript Scope
In JavaScript, scope is the set of variables, objects, and functions you
have access to.
31
JavaScript Scope (cont’d)
32
JavaScript Events
HTML Events
An HTML event can be something the browser does, or something a user does.
An HTML web page has finished loading.
An HTML input field was changed.
An HTML button was clicked.
HTML allows event handler attributes, with JavaScript code, to be
added to HTML elements. Try it! Try it! Try it!
With single quotes
<some-HTML-element some-event=‘some JavaScript’>
33
JavaScript Events (cont’d)
34
JavaScript Strings
JavaScript Strings
Stores a series of characters like “John Doe”.
Can be any text inside (single or double) quotes. Try it! Try it!
String Length
Built in property length Try it!
35
JavaScript Strings (cont’d)
36
JavaScript Strings Methods
37
JavaScript Strings Methods (cont’d)
38
JavaScript Numbers
Precision
Integers are considered accurate up to 15 digits. Try it!
The maximum number of decimals is 17. Floating point arithmetic is not
always 100% accurate. Try it!
Infinity
Infinity (or –Infinity) is the value. JavaScript will return if you calculate a
number outside the largest possible number. Try it! Try it! Try it!
39
JavaScript Numbers (cont’d)
NaN – Not a number Try it! Try it! Try it!
JavaScript reserved word indicating that the result of a numeric operation was not
a number.
Numbers Can be Objects Try it!
Number Properties and Methods
Number Properties
Global Methods
Can be used on all JavaScript data types
Try it!
Try it!
Try it!
41
JavaScript Numbers Methods (cont’d)
Number Methods
Can be used on numbers.
Try it!
Try it!
Try it!
Try it!
Try it!
42
JavaScript Math Object
43
JavaScript Math Object (cont’d)
44
JavaScript Dates
45
JavaScript Dates (cont’d)
Displaying Dates
toString( ) Try it!
toUTCString( ) Try it!
toDateString( ) Try it!
Try it!
47
JavaScript Date Methods (cont’d)
Try it!
Try it!
Creating an Array
Syntax
var array-name = [item1, item2, …] ;
Example
var car= [“Saab”, “Volvo”, “BMW”] ;
49
JavaScript Arrays (cont’d)
50
JavaScript Arrays (cont’d)
51
JavaScript Array Methods
Shifting Elements
shift ( ) Try it!
unshift ( ) Try it!
52
JavaScript Array Methods (cont’d)
Changing Elements Try it! Try it!
Splicing an Array
splice ( ) Try it! Try it!
Sorting an Array
sort( ) Try it!
Reversing an Array
reverse( ) Try it!
53
JavaScript Array Methods (cont’d)
54
JavaScript Booleans
55
JavaScript Booleans (cont’d)
56
JavaScript Comparison and Logical Operators
Comparison Operators
Given that x=5
57
JavaScript Comparison and Logical Operators (cont’d)
Logical Operators
Given that x=6 and y=3
58
JavaScript if…Else Statements
The if statement
To specify a block of JavaScript code to be executed if a condition is true.
Try it!
59
JavaScript Switch Statement
60
JavaScript For Loop
61
JavaScript Break and Continue
62
JavaScript Errors
63
JavaScript Errors (cont’d)
64
<< Here is the end of “’For’ Loops in JavaScript” in Codecademy.com. >>
65