Computer Programming.
net 1&2
Reviewer for 2nd Quarter
Javascript
JavaScript is one of the 3 languages all web developers must learn:
1. HTML to define the content of web pages
2. CSS to specify the layout of web pages
3. JavaScript to program the behavior of web pages.
JavaScript and Java are completely different languages, both in concept and design.
JavaScript Functions and Events
A JavaScript function is a block of JavaScript code, that can be executed when "called" for.
For example, a function can be called when an event occurs, like when the user clicks a button.
JavaScript Programs
A computer program is a list of "instructions" to be "executed" by the computer. In a programming
language, these program instructions are called statements. JavaScript is a programming language.
JavaScript statements are separated by semicolons
JavaScript Values
The JavaScript syntax defines two types of values: Fixed values and variable values. Fixed values are
called literals. Variable values are called variables.
JavaScript Variables
In a programming language, variables are used to store data values. JavaScript uses the var keyword to
declare variables. An equal sign is used to assign values to variables.
JavaScript Operators
JavaScript uses arithmetic operators ( + - * / ) to compute values:
Example: (5 + 6) * 10
JavaScript uses an assignment operator ( = ) to assign values to variables:
Example:
var x, y; x = 5; y = 6;
JavaScript Expressions An expression
is a combination of values, variables, and operators, which computes to a value. The computation is
called an evaluation.
For example, 5 * 10 evaluates to 50:
JavaScript Keywords
JavaScript keywords are used to identify actions to be performed. The var keyword tells the browser to
create variables:
JavaScript Comments
Not all JavaScript statements are "executed". Code after double slashes // or between /* and */ is
treated as a comment. Comments are ignored, and will not be executed.
JavaScript, identifiers
are used to name variables (and keywords, and functions, and labels). The rules for legal names are
much the same in most programming languages. In JavaScript, the first character must be a letter, or an
underscore (_), or a dollar sign ($). Subsequent characters may be letters, digits, underscores, or dollar
signs. All JavaScript identifiers are case-sensitive.
JavaScript Data Types
JavaScript can handle many types of data, but for now, just think of numbers and strings. Strings are
written inside double or single quotes. Numbers are written without quotes, and If you put a number in
quotes, it will be treated as a text string
A variable declared without a value will have the value undefined.
The modulus operator (%) returns the division remainder.
var x = 5; var y = 2; var z = x % y
Incrementing
The increment operator (++) increments numbers.
var x = 5; x++; var z = x;
Decrementing
The decrement operator (--) decrements numbers.
var x = 5; x--; var z = x;
Operator Precedence
Operator precedence describes the order in which operations are performed in an arithmetic
expression.
var x = 100 + 50 * 3;
The assignment (=) operator is used to assign a value to a variable. The assignment operation
evaluates to the assigned value. Chaining the assignment operator is possible in order to assign
a single value to multiple variables.
JavaScript Strings
A string (or a text string) is a series of characters like "John Doe". Strings are written with quotes. You
can use single or double quotes.
var carName = "Volvo XC60"; // Using double quotes
var carName = 'Volvo XC60'; // Using single quotes
Booleans
can only have two values: true or false. Booleans are often used in conditional testing.
Example: Result: var x = 5; var y = 5; var z = 6; (x == y) // Returns true (x == z) // Returns false
Function parameters are listed inside the parentheses () in the function definition. Function arguments
are the values received by the function when it is invoked. Inside the function, the arguments (the
parameters) behave as local variables.
Function Invocation
The code inside the function will execute when "something" invokes (calls) the function:
- When an event occurs (when a user clicks a button)
-When it is invoked (called) from JavaScript code –
Automatically (self invoked)
Using Functions
When JavaScript reaches a return statement, the function will stop executing. If the function was
invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.
Functions often compute a return value. The return value is "returned" back to the "caller"
JavaScript Object
is the JavaScript element that is like a variable but contains many values
Object Methods
Objects can also have methods. Methods are actions that can be performed on objects. Methods are
stored in properties as function definitions.
String Properties and Methods String
Have their own built-in variables and functions, also known as properties and methods. Here are some
of the most common ones:
Length – a property that keeps track of how many characters a string has
toLowerCase() – a method that returns a copy of the string with its letters converted to lowercase
toUpperCase() – a method that returns a copy of the string with its letters converted to capitals
trim() - returns a copy of the string with beginning and ending whitespace characters removed
JavaScript Number
In JavaScript, numbers are values that can be used in mathematical operations. You don’t need any
special syntax for numbers — just write them straight into JavaScript (Example: 12345;).
JavaScript Arrays
In JavaScript, an array is an ordered list of values. Arrays are container-like values that can hold other
values. Each value is called an element specified by an index.
Array Properties and Methods
JavaScript Boolean
Booleans are values that can be only one of two things: true or false. It’s useful to store booleans in
variables to keep track of their values and change them over time.
Comparisons and Conditions
The Boolean value of an expression is the basis for all JavaScript comparisons and conditions.
Logical Operators
Logical operators are used to determine the logic between variables or values. Given that x = 6 and y =
3, the table below explains the logical operators:
JavaScript Conditions
Conditional statements are used to perform different actions based on different conditions.
Conditional Statements
The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
Example:
if (condition) { block of code to be executed if the condition is true }
The else Statement
Use the else statement to specify a block of code to be executed if the condition is false
Example:
. if (condition) { block of code to be executed if the condition is true }
else
{ block of code to be executed if the condition is false }
The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Example:
if (condition1) { block of code to be executed if condition1 is true }
else if (condition2)
{ block of code to be executed if the condition1 is false and condition2 is true }
else
{ block of code to be executed if the condition1 is false and condition2 is false }
JavaScript Loops
A loop is a sequence or a series of instructions that is continually repeated until a certain condition is
met.
There are different kinds of Loops in JavaScript, those are;
for - via block of code a number of times.
for/in - via properties of an object.
while - via block of code while a specified condition is true. Execute the program after checking the
condition.
do/while - via block of code while a specified condition is true. Execute the program before checking
the condition.
For Loop
It is a block of code a number of times.
Syntax: for (statement 1; statement 2; statement 3) { code block to be executed }
While Loop
Syntax: while (condition) { code block to be executed }
Example:
The code in the lo op will run repeatedly as long as a variable “j” is less than 11. Th e condition is j < 11.
Do while Loop
In this loop, the program will always be executed at least once, even if the condition is false because the
code block is executed before checking the condition. Syntax: do { code block to be executed } while
(condition);