You are on page 1of 8

Java script is a browser-based programming language with object-oriented capabilities that allows one

to build interactivity into HTML pages. In other words, it is written directly into the HTML web pages and
is interpreted and executed in response to some activists on the page such as when loading or when a
button is clicked. Java script interacts with the browsers and can read information of the web page e.g.,
the data entered in the form.

It can be incorporate din a web page in 4 ways

1. Written into the body of page using the script tag


2. As an attached code written into a html tag
3. As a set of functions written in the main area.
4. As a set of function written as a separate tag and brought into the page through a link.

It is case sensitive

ADVANTAGES

• Is easy to learn and user friendly because it has increased interactivity, you can create interfaces
that react when the users hover over them.
• Has less server interaction. One can validate user input before sending the page of to the server,
which means less load on the server.
• It is embedded in html and uses small routine or tasks to create special effects.
• Richer interfaces, JavaScript can include items such as drag and drop components to give a rich
interface to the site visitors.

LIMITATIONS

• It is a client-side language and allows the reading and writing of files.


• It cannot be used for networking applications because there is no such support available

DEVELOPMENT TOOLS USED IN JS

Js has development tools that can help one develop the course.

• Editor which is the notepad when you can develop the js code
• Microsoft front page which provides web developers with a number of js tools to assist in the
creation of an interactive website.
• Micro media dream weaver – provides prebuilt js components that integrates well with the
databases.
• Micro media home site- provides html and js editor which manage personal websites.

Js variables
Variables are simply named containers that hold data. Variables in js are declared with the
keyword var. E.g.
Var num1, var num2, Var total
When naming variables the following rules must be taken into consideration
1. You should not use any js reserved words . e.g. break , true , Boolean are not valid
names.
2. Variables names should not start with a numeral e.g. 0-9 , must begin with a letter or
underscore character.
3. They are case sensitive. E.g name and name are different variables .
4. Variable scope. Scope of a variable is the region in each a variable are defined. Js has
two scopes. Global variable which means it is defined to be used everywhere in your js
code. Local variable is visible only within a function where it is defined

JS RESERVED WORDS

This are words that cannot be used as js variables, functions, methods ,loop ,labels or object names.

Some of them include, var, void ,while, switch, this , throw, return, long, int, for , function, go to, car,
break, case , continue ,do ,double etc,

JS DATA TYPES

Numbers which are represented in the step and can also read octal and hexadecimal.

String Data in form of characters such as name ,country etc,

Boolean- True or false

Operators used in js

Include arithmetic operators which include plus , minus, multiplication, increment, decrement

Comparison operators which acts as and equal sign = , Not equal, greater or equal, less or equal.

Logical operators include AND AND NOT OPERATOR

Assignment operators include equal, plus, equal, minus equal, multiplication equal, less equal

Other operators include conditional operator and the type of operator

DISPLAYING OF VARIABLES IN JS

Variables can be displayed in text boxes or forms in the dialog boxes, document.write or print
statement

E.g. To display the sum of 2 numbers

Document. Write (“the total is “ + total)

This statement will display the value of the sum or the total on page.
Variables can be declared and assigned values directly.

e.g.

<!DOCTYPE html>

<html>

<head>

<title>js</title>

<script type="text/javascript">

var x= 200;

var y= 300;

var total=x+y;

document.write("The total "+ total);

</script>

</head>

<body>

</body>

</html>

Alert keyword can also be used to display the information in the screen.

To receive variable In a dialog box , the keyword prompt is used in the statement, and parse int is used
in the statement to convert a sequence of characters into its integer values.

e.g.

var num1, num2,var sum,

num1=prompt(“ Enter first number”)

num2=prompt(“Enter second number”)

sum = parseInt(num1 )+ parseInt(num2)

alert (sum)
<!DOCTYPE html>

<html>

<head>

<title>js</title>

<script type="text/javascript">

var x= prompt("Enter first number");

var y= prompt("Enter Second number");

var total= parseInt(x)+parseInt(y);

var average;

alert("The total "+ total + " and avg is "+ total/2);

</script>

</head>

<body>

</body>

</html>
Control structures are used to determine program flow or order in which the instruction are carried out.

They include, sequence selection, or interaction,

With the aid of a flow diagram describe the following sequence

Sequence, iteration, Selection.

Control structures mainly us comparison operators, they are operators used to compare the content of
variables with the values of others. They also use the logical operators used to check if a variable fall into
a range of values.

The control structures use the conditional statements and the conditional statements includes

If else, if then else, Nested if else statement

e.g.

write a program that prompt for the age of a person

if the age is less than 18 , then display you are too young, else if greater than 18, you are too old.
<!DOCTYPE html>

<html>

<head>

<title>js</title>

<script type="text/javascript">

var age=parseInt(prompt("Enter age"));

if(age<18){

alert("You are too young but determined");

}else if(age>=18){

alert("Welcome to the league");

</script>

</head>

<body>

</body>

</html>

<!DOCTYPE html>

<html>

<head>

<title>Colors </title>

<script type="text/javascript">

var colors=prompt("Enter a color");

if (colors=="green"){

alert("Or country");

}else if(colors=="black"){

alert("too dark");

}else if(colors =="red"){

alert("Danger");

else{

alert("Not available");

</script>

</head>

<body>

</body>

</html>
Case statement is used instead of the if then if statement

Case statement

Switch (expression)

{case1:statemet
Iterations mainly uses loops and loops enables a statement to be repeated several times

There are various types of loops or iterations statements that can be used. This include

For loop, while loop etc.

For loop

Enables initialization of the variable and incrementing of the variable

Its syntax is as follows

Write a for loop to display number between 5 and 10

Loops can be nested with the inner loop running the full force each time the program .

It can be used to read arrays of two or more dimensions, e.g.

For(row=1;row<=10; row++)

For (row=1 ; col<=10; col++)

WHILE LOOP

Can also be used to repeat a number of statements. If the condition is met at the start of the loop. The
statements are not executed at all, its main aim is to execute a statement of code repeatedly as long
as the expression is true. Once expression becomes false. The loop exits .

Its syntax is as follows.


Initialization

While(statement)

increment

e.g.

While (count <=10)

Count++

Document. Write (“Count = “+ count)

Do while loop performs its tests after the enclosed statement, its syntax is as follows

Do{

Statement

While (test)

Var i = 10

You might also like