Skip to content
UoL CS Notes

JavaScript Introduction

COMP284 Lectures

JavaScript is a language for client-side programming.

Hello, world!

<!DOCTYPE html>
<html lang="en-GB">
<head>
	<title>Hello, world!</title>
</head>
<body>
	<script>
		document.writeln("<p><b>Hello, world!</b></p>");
	</script>
	<noscript>
		<p><b>Please enable JavaScript to see the super secret message!</b></p>
	</noscript>
</body>
</html>
  • Script is enclosed in script tags.
  • Alternative HTML is enclosed in noscript tags.

Types

JavaScript is a dynamically and loosely typed language. It has five different types:

  • boolean
  • number
    • This is both integers and floating point numbers are treated the same.
    • We can use the Math object (Math.log()) to do mathematical functions.
  • string
  • function
  • object

We can find the type of an object like so:

document.writeln("Type of 23.0: " + typeof(23.0));

Typecasting

We can cast types by wrapping them in an appropriate object from:

  • Number()
  • String()
  • Boolean()

We can also process specifically as an int or float:

  • parseInt()
  • parseFloat()

Strings

We can write strings like so:

document.writeln("Your name is " + name + "and\
	you are studying " + degree + "\
	at " + university);

Variables

Variables can be declared like so:

var variable1 = value, variable2 = value2, ...

If you don’t initialise a variable before using it you will get a reference error.