COMP284 Scripting Languages – Handouts
COMP284 Scripting Languages
Lecture 14: JavaScript (Part 1)
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
1 JavaScript
Motivation
Overview
Example
2 Types and Variables
Types
Variables
Typecasting
Comparisons
COMP284 Scripting Languages Lecture 14 Slide L14 – 1
JavaScript Motivation
JavaScript: Motivation
• PHP and Perl both allow us to create dynamic web pages
• In web applications, PHP and Perl code is executed on the web server
(server-side scripting)
• allows to use a website template that is instantiated using data stored in a
database
• ‘business logic’ is hidden from the user:
the code of an application is not visible to the user/client;
the user/client only has access to the HTML produced by the code
• not ideal for interactive web applications:
too slow to react and too much data needs to be transferred
• operations that refer to the location of the user/client are difficult,
for example, displaying the local time
echo date(’H:i l, j F Y’);
displays the local time on the server not the local time for the user
COMP284 Scripting Languages Lecture 14 Slide L14 – 2
JavaScript Overview
JavaScript
• JavaScript is a language for client-side scripting
• script code is embedded in a web page (as for PHP),
but 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 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 (not the server’s)
• operations that refer to the location of the client are easy:
document.write(“Local time: ” + (new Date). toString ());
COMP284 Scripting Languages Lecture 14 Slide L14 – 3
JavaScript Overview
JavaScript: History
• originally developed by Brendan Eich at Netscape
under the name Mocha
• first shipped together with Netscape browser in September 1995
under the name LiveScript
• obtained its current name in December 1995 under a deal between
Netscape and Sun Microsystems, the company behind Java,
in December 1995
• does not have a particularly close relationship to Java,
it mixes aspects of Java with aspects of PHP and Perl
and its own peculiarities
• is a dialect of ECMAScript, a scripting language standardised in the
ECMA-262 specification and ISO/IEC 16262 standard since June 1997
• other dialects include Microsoft’s JScript and TypeScript and
Adobe’s ActionScript
COMP284 Scripting Languages Lecture 14 Slide L14 – 4
JavaScript Overview
Websites and Programming Languages
Website Client-Side Server-Side Database
Google JavaScript C, C++, Go, Java,
Python, PHP
BigTable, MariaDB
Facebook JavaScript Hack, PHP, Python,
C++, Java, . . .
MariaDB, MySQL,
HBase Cassandra
YouTube Flash,
JavaScript
C, C++, Python, Java,
Go
BigTable, MariaDB
Yahoo JavaScript PHP MySQL, PostgreSQL
Amazon JavaScript Java, C++, Perl Oracle Database
Wikipedia JavaScript PHP, Hack MySQL, MariaDB
Twitter JavaScript C++, Java, Scala MySQL
Bing JavaScript ASP.NET MS SQL Server
Wikipedia Contributors: Programming languages used in most popular websites. Wikipedia, The Free Encyclopedia,
20 October 2017, at 11:28. http://en.wikipedia.org/wiki/Programming_languages_used_in_most_popular_websites
[accessed 23 October 2017]
COMP284 Scripting Languages Lecture 14 Slide L14 – 5
http://en.wikipedia.org/wiki/Programming_languages_used_in_most_popular_websites
JavaScript Example
JavaScript: Hello World!
1
2
3
Our first JavaScript script
4 7
10
• JavaScript code is enclosed between
• Alternative HTML markup that is to be used in case JavaScript is not
enabled or supported by the web browser, can be specified between
• File must be stored in a directory accessible by the web server, for
example $HOME/public_html, and be readable by the web server
• No particular file name extension is required
COMP284 Scripting Languages Lecture 14 Slide L14 – 6
JavaScript Example
JavaScript scripts
• JavaScript scripts are embedded into HTML documents and are
enclosed between