JavaScript Handout
JavaScript Handout
Learning objectives
I. Introduction
1.1 Definition
JavaScript is a very powerful client-side scripting language. JavaScript is used mainly for
enhancing the interaction of a user with the webpage. In other words, you can make your webpage
more lively and interactive, with the help of JavaScript. JavaScript is also being used widely in game
development and Mobile application development.
JAVA JAVASCRIPT
Java is strongly typed language and variable must JavaScript is weakly typed language and have
be declare first to use in program. In Java the type more relaxed syntax and rules.
of a variable is checked at compile-time.
Java is an object oriented programming language. JavaScript is an object based scripting language.
Java applications can run in any virtual JavaScript code run on browser only as JavaScript
machine(JVM) or browser. is developed for browser only.
Java is strongly typed language and variable must JavaScript is weakly typed language and have
be declare first to use in program.In Java the type more relaxed syntax and rules.
of a variable is checked at compile-time.
Java is an object oriented programming language. JavaScript is an object based scripting language.
It is really impossible to give a complete list of all the available JavaScript frameworks and libraries. The
Javascript world is just too large and too much new is happening.
2 JAVASCRIPT
Exercise : Create a Javascript file called one.js which does sets a as 5 and then display the value of a on
an HTML page.
2.1.2 JavaScript Comments
Comments are used to describe part of a block of codes and comments do not affect the execution of the
code. There are two types of comments in JavaScript which are Single line and multi-line comments.
abstract else instanceof super abstract else instanceof super abstract else
boolean enum int switch boolean enum int switch boolean enum
break export interface synchronized break export interface synchronized break export
byte extends let this byte extends let this byte extends
case false long throw class false long throw case false
catch final native throws catch final native throws catch final
String Assignment
It is used to assign a string to variable on the left hand.
Ex: name ='john' or sex="boy". You can use 'data' or "data", both will work.
To join(concatenate) a string with another variable of string, we use the +.
Example: alert(name+' is a '+sex+' years old');
Web Programming 1 - JAVASCRIPT (Part 1) – Mr. Nyambi Blaise (679194380) Page 8 of 13
Topic: JavaScript (Part 1) By: Nyambi Blaise
<script>
message = 'hello ';
message2 = "HND ";
level = 1;
name = "Paul Richards";
age = 20;
phone = 678237923;
console.log(name+" is "+age+" years old and his phone number is "+phone);
document.write(name+" is "+age+" years old and his phone number is "+phone);
alert(name+" is "+age+" years old and his phone number is "+phone);
</script>
In the example above, we have defined variables of type integers and strings and displayed
them using the various ways through which data can be displayed in JavaScript. We also concatenated a
string with other data.
You can decide how you would want to display your data in your environment testing when working on
your practical but you should understand how the console, alert and document.write work and when you
need to use any of them.
2.3.3 Arithmetic Operators
JavaScript supports the following arithmetic operators:
➢ Addition (+), adds 2 or more values or concatenates a string to another value or variable
➢ substraction(-), subtracts a value from another value
➢ multiplication(*), multiplies 2 or more valus
➢ division(/), divides the numerator by the denominator
➢ modulus(%), returns the output of an integer division
➢ increment(++), increments a value by 1
➢ decrement(--), decrements a value by 1
Exercise: Use JavaScript to demonstrate each of the arithmetic operations seen above.
2.3.4 Comparison Operators
These operators are used to compare 2 or more values. They are used mostly with the if condition or as
Booleans which returns either true if the condition is true or false if the condition is false. There are 6
main comparison operators: lets assume a=50 and b=42
➢ Equal (==), Checks if the value of two operands are equal or not, if yes, then the condition
becomes true. (a==b) is not true
<script>
function confirmToClose() {
confirm("Sur To Close?!");
confirm("Sur To Close?"); }
</script>
<a href="" onclick="confirmToClose()">Close
Window</a>
<script>
function confirmToClose() {
var check = confirm("Sur To Close?!");
if(check==true)
{
alert("Bye Bye");
return true;
} else {
alert("You cancelled");
return false;
}
}
</script>
<a href="" onclick="confirmToClose()">Close Window</a>
This dialog box has two buttons: OK and Cancel. If the user clicks the OK button, the window method
prompt() will return the entered value from the text box. If the user clicks the Cancel button, the window
method prompt() returns null.
<script>
function askQuestion() {
prompt("Enter Your Name?"); var name = prompt("Enter Your Name");
alert("Welcome "+name);
}
</script>
<a href="" onclick="askQuestion()">Go</a>