JavaScript (Part 1)- Introduction
JavaScript (Part 1)- Introduction
Ullrich Hustadt
1 JavaScript
Motivation
Overview
Example
Scripts
Type Systems and Type Checking
2 Further Reading
JavaScript: Motivation
Scripting languages
Script
A user-readable and user-modifiable program that performs simple
operations and controls the operation of other programs
Scripting language
A programming language for writing scripts
JavaScript
• JavaScript is a language for client-side scripting
• script code is embedded in a web page and
delivered to the client as part of the web page and
executed by the user’s web browser
; code is visible to the user/client
• allows for better interactivity compared to server-side scripting as reaction
time is improved and data exchange with the server can be minimised
• a web browser may not support JavaScript or
the user may have disallowed the execution of JavaScript code
• different JavaScript engines may lead to different results, in particular,
results not anticipated by the developer of JavaScript code
• performance relies on the efficiency of the JavaScript engine and
the client’s computing power
JavaScript
• JavaScript is a language for client-side scripting
• operations that refer to the location of the client are easy:
document . write (" Local time : " + ( new Date ). toString ());
• for security reason, scripts are limited in what they can do,
e.g. scripts cannot access the client’s filestore
JavaScript: History
JavaScript: Use
JavaScript scripts
• JavaScript scripts are embedded into HTML documents and are
enclosed in a script-element
• A JavaScript script consists of one or more statements and comments
; there is no need for a main function (or classes)
• Statements do not have to end in a semicolon but they can
; stick to one convention in your code
• Whitespace before and in-between statements is irrelevant
(This does not mean it is irrelevant to someone reading your code)
• One-line comments start with // and run to the end of the line
// This is a single - line comment .
Value Type
COMP519 Web Programming Lecture 10 Slide L10 – 13
JavaScript Type Systems and Type Checking
Type System
A set of rules that determines the type of a value, variable, operation,
expression possibly taking into account type declarations for variables and
operations
Type Error
A failure of a type system to determine the type of a value, variable,
operation, expression
Type Checking
The process of checking that a ‘program’ complies with the type system of
its language
f ();
python TEE . py
Called f ()
T r a c e b a c k ( most r e c e n t c a l l l a s t ) :
F i l e ”TEE . py ” , l i n e 5 , i n <module>
f ();
F i l e ”TEE . py ” , l i n e 3 , i n f
p r i n t (”0” / 1 ) ;
T y p e E r r o r : u n s u p p o r t e d o p e r a n d t y p e ( s ) f o r / : ’ s t r ’ and ’ i n t ’
Now that Python attempts to evaluate "0" / 1, a type error is indicated
COMP519 Web Programming Lecture 10 Slide L10 – 18
JavaScript Type Systems and Type Checking