What is Javascript

Javascript is the scripting language of the web that allows you to create dynamic websites. You can create slide shows, games, validate forms and so forth with javascript.

How to create a script


To begin writing code in javascript, type the code highlighted in red in the head of your html file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Assignment 0</title>

<script type="text/javascript">

</script>

</head>
<body>
</body>
</html>

How to display data or information on the screen

To display anything, use document.write() or document.writeln()

Example 1: Write a javascript that displays your name

<script type="text/javascript">
document.write(“Nelson Mandela”);
</script>

Note that the name must be enclosed in quotation marks

Example 2: Write a script that displays Good Morning

<script type="text/javascript">
document.write(“Welcome to Urban Science”);
</script>

Variables

A variable is a memory location that can hold any value such as number, text or string, date and etc. Javascript variable are a lot like math variables. They hold values.

How to declare a variable in Javascript

Use the keyword var followed by the variable name. The variable name can be anything such as x, y, z. However, we have to be more descriptive. For instance, if you want to store a name. You should use name as the variable name instead of x.

Example 3: Write a program that stores a person’s name in a variable and display the name in the variable.

<script type="text/javascript">
var name = “Joe”;
document.write(name);
</script>

Notice that the name is not enclosed in quotation marks because it is not a string or text, it is a variable. Variables and numbers should not be enclosed in quotation marks.

Obtaining user input

To collect data from a vistor, use the prompt () function. This function returns a value; therefore you must capture that return value using a variable.

Example 4: Write a program that asks the user for his/her name and displays the name.

<script type="text/javascript">
var name;
name = prompt("What is your name?");
document.write(name);
</script>

Example 5: Write a program that asks a user for his/her first name and then his/her last name. Display that information on the screen.

<script type="text/javascript">
var firstname;
var lastname;
firstname = prompt(“Enter your first name “);
lastname = prompt(“Enter your last name”);

document.writeln(firstname);
document.writeln(lastname);
</script>

Concatenate strings or text

Concatenate strings mean to combine two strings
To concatenate strings, use the + sign

Example 6: Write a program that asks a user for his/her first name and then his/her last name. Display that information on the screen by concatening the first name and the last name.

<script type="text/javascript">
var firstname;
var lastname;
firstname = prompt(“Enter your first name “);
lastname = prompt(“Enter your last name”);
do cument.writeln(firstname + lastname);
</script>

Converting strings into numbers

In programming, any data that you collect from a user is interpreted as text or string by the computer, therefore you must convert that data into number.

To convert data into number, use the parseInt() function.

Example 7: Write a program that asks a user for two numbers , computes the total and display the total.
<script type="text/javascript">
var number1;
var number2;
var total;
number1 = prompt("enter the first number");
number2= prompt("Enter the second number");
total = parseInt(number1) + parseInt(number2);
document.write(total);
</script>