Académique Documents
Professionnel Documents
Culture Documents
2
What is JavaScript?
Object-based scripting language
3
What is JavaScript?
Originally developed by Netscape
◦ Named LiveScript
4
Differences between JavaScript and Java
interpreted, not compiled
more relaxed syntax and rules
◦ fewer and "looser" data types
◦ variables don't need to be declared
◦ errors often silent (few exceptions)
key construct is the function rather than the
class
◦ (more procedural less object-oriented)
contained within a web page and integrates with
its XHTML/CSS content
Common Uses of JavaScript
Display a message box
Select list navigation
Edit and validate form information
Create a new window with a specified size
and screen position
Image Rollovers
Status Messages
Display Current Date
Calculations
6
Coding JavaScript
JavaScript statements can be coded on a
Web page using two different techniques:
Variables
◦ Start with $, _, letter
◦ Case sensitive
Statements can be terminated with a
semicolon
Reserved words (25 words)
Comments (same as Java)
◦ //
◦ /* … */
PRIMITIVES,
OPERATIONS AND
EXPRESSIONS
Primitive Types
Five primitive types
◦ Number
◦ String
◦ Boolean
◦ Undefined
◦ Null
There are five objects corresponding to the five
primitive types
◦ These are called Wrapper objects for primitive
values
◦ Primitive values are coerced to the wrapper class as
necessary, and vice-versa
Numeric Literals (or type)
Number values are represented internally
as double-precision floating-point values
◦ integers and real numbers are the same type
(no int vs. double)
◦ Float values may have a decimal and/or
exponent
◦ same operators: + - * / % ++ -- = += -= *= /=
%=
◦ similar precedence to Java
String Literals (or type)
A String literal is delimited by either
single or double quotes
◦ There is no difference between single and
double quotes
◦ Certain characters may be escaped in strings
\‟ or \” to use a quote in a string delimited by the same quotes
\\ to use a literal backspace
Other Primitive Types
Null
◦ A single value, null
◦ null is a reserved word
◦ an assignment value, It can be assigned to a variable as a
representation of no value.
◦ Using a null value usually causes an error
Undefined
◦ A single value, undefined
◦ However, undefined is not, itself, a reserved word
◦ The value of a variable that is declared but not assigned a value
Boolean
◦ Two values: true and false
Declaring variables
var name = expression;
----------------------------------------------------------
var clientName = “abc xyz";
var age = 32;
var weight = 45;
-----------------------------------------------------------
declaring variables:
◦ explicitly, by var keyword (case sensitive)
◦ implicitly, by assignment (give it a value, and it exists!)
types: not specified, but JS does have types ("loosely
typed")
◦ common types: Number, Boolean, String, Null, Undefined
◦ can find out a variable's type by calling typeof
The Math Object
Provides a collection of properties and
methods useful for Number values
This includes the trigonometric functions
such as sin and cos
methods: abs, ceil, cos,
floor, log, max, min, pow,
random, round, sin, sqrt,
tan
properties: E, PI
The Number Object
Properties
◦ MAX_VALUE
◦ MIN_VALUE
◦ NaN
◦ POSITIVE_INFINITY
◦ NEGATIVE_INFINITY
◦ PI
Operations resulting in errors return NaN
◦ Use isNaN(a) to test if a is NaN
toString method converts a number to string
The String Object
var s = "Connie Client";
var fName = s.substring(0, s.indexOf(" ")); // “Connie"
var len = s.length; // 13
var question =
confirm("Do you want to continue this download?");
The prompt Method
This method displays its string argument in a dialog box
◦ A second argument provides a default content for the user entry area
The dialog box has an area for the user to enter text
The method returns a String with the text entered by the user