Javascript 1
Javascript 1
Introduction
• Object-oriented scripting language that is dynamic, weakly
typed and has first-class functions.
• used in the form of client-side JavaScript, implemented as part
of a web browser in order to provide enhanced user interfaces
and dynamic websites
JavaScript:
coding errors
Variables
• A variable is a name associated with a piece of data
programs
information
Variables
• In JavaScript variables are • Example:
created using the keyword var
• var x = 10;
• var y = 17;
name of the variable and red is the value. In other words, color is
the name of the box while red is what is inside the box
Data Types
Primitive Data Types
• Numbers
• Strings
• Arrays
• Objects
Primitive Data Types
• Numbers - A number can be either an integer or a decimal
contexts
Implicit Data Types
• Although JavaScript does not have explicit data types, it does
evaluate to a number
var q = “17”;
Variable Names
• JavaScript is case sensitive
a digit
an number.
postfix y = 11
program
Repeat Loops
• A repeat loop is a group of statements that is repeated until a
for more efficient program design and are ideally suited for
count++;
}
The For Loop
• The for loop is used when there is a need to have a counter of
some kind
• The counter is initialized before the loop starts, tested after each
<SCRIPT <SCRIPT
LANGUAGE= LANGUAGE=
"JavaScript"> "JavaScript">
document.write("1");
document.write("2");
for (i=1; i<=5; i++)
document.write("3");
document.write(i);
document.write("4");
document.write("5");
</SCRIPT>
Functions
a specified task
operation
Functions
• Functions have inputs and outputs
• The inputs are passed into the function and are known as
arguments or parameters
operation
Defining Functions
• The most common way to define a function is with the function
statement.
z = 3; Input/Argument: x
sqr_z = square(z);
Output: x*x
Example: Function
function sum_of_squares(num1,num2)
{return (num1*num1) + (num2*num2);}
function sum_of_squares(num1,num2)
{return (square(num1) + square(num2));}