CS1315: Introduction to Media Computation “What do other languages look like? How hard is it to do Web programming?”: JavaScript
What do other languages look like?
We call the language “look” its syntax Python is a fairly traditional language in terms of syntax.
Languages like Scheme and Squeak are significantly different.
Major points of difference:
Whether or not variables have to be declared before first use. Details of how individual lines are written. Details of how blocks are defined.
JavaScript
JavaScript is meant to be a scripting language, like Python.
Scripting languages are meant for non-professional programmers to solve simple tasks. It’s designed to look like Java to ease the transition in either way.
JavaScript can be used by the web server (used on the computer accessed via the Internet), or it can be used within an HTML page.
If it’s within the HTML page, it’s actually executed by the user’s browser. We call that client side JavaScript.
JavaScript syntax: Variables
Variables must be declared before use.
You can’t just say: a = 12 You can either say: var a = 12; Or: var a; a = 12
In other languages, you might also declare the variable’s type int a=12;
JavaScript syntax: Blocks
Blocks are delimited with curly braces.
function test() { document.writeln("This is a test"); }
JavaScript syntax: Individual statements
Lots of differences:
func tion instead of def End lines with semicolons “;”
(But lines can have returns in the middle of them.)
The for statement is numeric (mostly) and has different parts to it. You use write or writeln instead of pri nt
But they’re mostly detail changes.
The basic operation of JavaScript is not unlike Python.
JavaScript is all about objects
Just about every function is actually a method. For example, there is no global print. There is a function write or writeln
Writeln adds a new line (‘\n’) at the end.
But these aren’t global functions.
To write into the document, you use document. wri te() document. wri te() is a method on the HTML document itself.
Embedding JavaScript inside HTML
JavaScript sits inside of HTML pages.
You wrap <script> tags around the JavaScript.
You can have <script> tags in two kinds of places.
Inside the tags to define functions used elsewhere. Inside the body, where the scripts are actually executed.
Our Simple Web Page
The Simplest Possible Web Page A Simple Heading
This is a very simple web page.
Adding some simple JavaScript
The Simplest Possible Web Page <script> function test() { document.writeln("This is a test"); }
A Simple Heading
This is a very simple web page.
<script> test()
Going into detail on the function <script> function test() { document.writeln("This is a test"); }
A Simple Heading
This is a very simple web page.
<script> test()
Here’s a function named “test” with no inputs, that only writes out a string.
Here we execute the function.
Can also insert HTML <script> function insertHead() { document.writeln("
This is a test
"); }
A Simple Heading
This is a very simple web page.
<script> insertHead()