Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
49 views

Programming For The Web

Javascript uses values, operators, and expressions to perform programming tasks. Values can be numbers, strings, booleans, null, and undefined. Operators perform operations on values like arithmetic, comparison, and logical operations. Expressions combine values and operators to evaluate to a single value. Keywords have special meanings in Javascript. Comments are used to document code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Programming For The Web

Javascript uses values, operators, and expressions to perform programming tasks. Values can be numbers, strings, booleans, null, and undefined. Operators perform operations on values like arithmetic, comparison, and logical operations. Expressions combine values and operators to evaluate to a single value. Keywords have special meanings in Javascript. Comments are used to document code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Javascript Statements

Monday, February 27, 2023 3:58 PM

Values:
A value is a piece of data that can be stored and manipulated by a program. In JavaScript, there are
several types of values, including numbers, strings, booleans, null, and undefined. For example, the
number 5, the string "Hello, world!", and the boolean value true are all values that can be used in
JavaScript programs.

Operators:
An operator is a symbol or keyword in JavaScript that performs an operation on one or more values.
There are several types of operators in JavaScript, including arithmetic operators, comparison
operators, and logical operators. Arithmetic operators perform mathematical operations on
numbers, such as addition (+), subtraction (-), multiplication (*), and division (/). Comparison
operators compare two values and return a Boolean value, such as equality (==), less than (<),
greater than (>), and not equal (!=). Logical operators perform Boolean operations on values, such as
logical AND (&&), logical OR (||), and logical NOT (!).

Expressions:
An expression is any valid unit of code that can be evaluated to a value. An expression can be made
up of values, operators, and function calls. For example, the expression 2 + 3 is an expression that
evaluates to the value 5. Similarly, the expression "Hello, " + "world!" is an expression that evaluates
to the string "Hello, world!".

Keywords:
Keywords are reserved words in JavaScript that have special meanings and cannot be used as
variable names or function names. JavaScript has a number of keywords, including if, else, while, for,
function, var, let, and const. These keywords are used to create and control program flow, define
functions, and declare variables.

Comments:
Comments are used to add explanatory notes or remarks to your code. Comments are not executed
by the JavaScript interpreter and are ignored. Single-line comments start with // and continue to the
end of the line. Multi-line comments start with /* and end with */. Comments can be used to
document code, explain tricky logic, or leave notes to other developers who may be working on the
same code. Good comments can help improve the readability and maintainability of your code.

Programming for the web Page 1


Data Types
Monday, February 27, 2023 7:01 PM

Number:
The number data type in JavaScript represents numeric values. Numbers can be integers or floating-
point values. JavaScript provides several built-in functions for working with numbers, such as
Math.floor(), Math.ceil(), and Math.random(). In addition, JavaScript provides some special numeric
values, such as Infinity, -Infinity, and NaN (Not a Number).

String:
The string data type in JavaScript represents text values. Strings are enclosed in single quotes ('...') or
double quotes ("..."). JavaScript provides several built-in functions for working with strings, such as
string.length, string.charAt(), string.indexOf(), and string.split(). Strings can be concatenated using
the + operator, and special characters can be escaped using the \ character.

Boolean:
The boolean data type in JavaScript represents boolean values, which can be either true or false.
Boolean values are often used in conditional statements and loops to control program flow.
JavaScript provides several built-in functions for working with booleans, such as Boolean(), which
converts a value to a boolean, and logical operators such as && (logical AND), || (logical OR), and !
(logical NOT).

Array:
The array data type in JavaScript represents a collection of values, which can be of any type. Arrays
are enclosed in square brackets ([]), and individual values are separated by commas. JavaScript
provides several built-in functions for working with arrays, such as array.length, array.push(),
array.pop(), array.shift(), and array.unshift(). Arrays can be used to store lists of values, such as a list
of names or a list of numbers.

Object:
The object data type in JavaScript represents a collection of key-value pairs, where each key is a
string and each value can be of any type. Objects are enclosed in curly braces ({}) and each key-value
pair is separated by a comma. JavaScript provides several built-in functions for working with objects,
such as object.hasOwnProperty(), object.keys(), and Object.assign(). Objects can be used to store
complex data structures, such as a user profile or a shopping cart.

Programming for the web Page 2


Type Conversions
Monday, February 27, 2023 7:14 PM

Type conversions in JavaScript are used to convert values from one type to another. There are
several ways to convert values in JavaScript, such as using the Number(), String(), Boolean(), and
parseInt() functions. For example, the Number() function can be used to convert a string to a
number, and the String() function can be used to convert a number to a string.

Programming for the web Page 3


Variables and Arrays
Monday, February 27, 2023 7:14 PM

Variables:
Variables in JavaScript are used to store and manipulate values in a program. Variables can be
declared using the var, let, or const keywords. The var keyword is used to declare a variable with
function scope, while the let and const keywords are used to declare variables with block scope.
Variables can be assigned a value using the = operator, and the value of a variable can be accessed
using its name. In addition, variables can be used in expressions, function calls, and other parts of a
program.

Arrays:
Arrays in JavaScript are used to store collections of values, which can be of any type. Arrays are
declared using square brackets ([]), and values are separated by commas. Arrays can be accessed
using an index, which starts at 0. JavaScript provides several built-in functions for working with
arrays, such as array.length, array.push(), array.pop(), array.shift(), and array.unshift(). In addition,
arrays can be used in loops, conditional statements, and other parts of a program to store and
manipulate data. The items are of the same type.

Programming for the web Page 4


JavaScript Operators
Monday, February 27, 2023 7:19 PM

Arithmetic operators are used to perform mathematical operations on values. The basic arithmetic
operators include addition (+), subtraction (-), multiplication (*), and division (/). There are also
some more advanced arithmetic operators in JavaScript, such as the exponentiation operator (**),
which raises a number to a certain power, and the unary negation operator (-), which negates a
number.

Assignment operators are used to assign values to variables. The basic assignment operator is the
equals sign (=), but there are also compound assignment operators that combine an arithmetic
operation with an assignment. These operators are shorthand for performing an arithmetic
operation and then assigning the result to the same variable. For example, the += operator can be
used to add a value to a variable and then assign the result back to the variable.

String operators are used to concatenate strings. The basic string operator is the plus sign (+), which
can be used to concatenate two strings together. There is also the shorthand += operator, which can
be used to concatenate two strings together and assign the result to the first string.

So while arithmetic operators are used for performing mathematical calculations, assignment
operators are used for assigning values to variables, and string operators are used for concatenating
strings.

Programming for the web Page 5


Comparison Operators
Monday, February 27, 2023 7:26 PM

Comparison operators in JavaScript are used to compare two values and return a Boolean value
(true or false) based on the comparison. There are several different types of comparison operators,
including:

Equal to (==): This operator checks if two values are equal in value, but not necessarily in type. For
example, 5 == "5" would be true because the values are equal, even though one is a number and the
other is a string.

Equal value and equal type (===): This operator checks if two values are equal in both value and
type. For example, 5 === 5 would be true because both values are the same number and the same
type, but 5 === "5" would be false because the types are different.

Not equal to (!=): This operator checks if two values are not equal in value. For example, 5 != 10
would be true because the values are different.

Not equal value and not equal type (!==): This operator checks if two values are not equal in both
value and type. For example, 5 !== "5" would be true because the types are different.

Greater than (>): This operator checks if the first value is greater than the second value. For
example, 5 > 3 would be true because 5 is greater than 3.

Less than (<): This operator checks if the first value is less than the second value. For example, 5 < 10
would be true because 5 is less than 10.

Greater than or equal to (>=): This operator checks if the first value is greater than or equal to the
second value. For example, 5 >= 5 would be true because 5 is equal to 5.

Less than or equal to (<=): This operator checks if the first value is less than or equal to the second
value. For example, 5 <= 10 would be true because 5 is less than 10.

In summary, comparison operators in JavaScript are used to compare two values and return a
Boolean value based on the comparison. There are several different types of comparison operators,
including equal to, equal value and equal type, not equal to, not equal value and not equal type,
greater than, less than, greater than or equal to, and less than or equal to.

Programming for the web Page 6


Logical Operators
Monday, February 27, 2023 7:28 PM

AND (&&) returns true only if both expressions being evaluated are true. This means that if either
expression is false, the entire expression will be false. It is often used to check if two conditions are
both true before executing a block of code.

OR (||) returns true if at least one of the expressions being evaluated is true. This means that if
either expression is true, the entire expression will be true. It is often used to check if at least one of
two conditions is true before executing a block of code.

NOT (!) is used to negate a Boolean expression. It returns the opposite of the expression being
evaluated. This means that if the expression is true, NOT will return false, and if the expression is
false, NOT will return true. It is often used to check if a condition is not true before executing a block
of code.

In summary, AND (&&), OR (||), and NOT (!) are three logical operators in JavaScript that are used to
combine Boolean expressions and evaluate them as either true or false. They have different
functions and are used in different ways depending on the desired outcome of the code being
written.

Programming for the web Page 7


Conditional Operators
Monday, February 27, 2023 7:29 PM

if statement: The if statement evaluates the condition in the parentheses to determine if it is true or
false. If the condition is true, the block of code inside the curly braces is executed. If the condition is
false, the block of code is skipped and execution continues with the next statement. The condition
can be any expression that evaluates to true or false, such as a comparison or a logical expression.

else statement: The else statement is used in conjunction with the if statement to execute a block of
code if the condition in the if statement is false. It does not have a condition of its own, but simply
executes if the preceding if statement condition is false. The else block of code is executed only if
the if block of code is not executed.

else if statement: The else if statement is used when there are multiple conditions to be checked. It
is used in conjunction with the if statement and the else statement. It allows for a series of
conditions to be checked, with each condition being checked only if the preceding conditions are
false. The else if statement is optional, and any number of else if statements can be used. The final
else block of code is executed only if none of the preceding conditions are true.

switch statement: The switch statement is used when there are multiple conditions to be checked
and the value being checked is the same. It is similar to a series of else if statements, but is more
concise and easier to read. The switch statement takes an expression as its argument, and compares
the value of the expression to the values in the case statements. The case statements are followed
by a colon and a block of code to execute if the expression matches the value. The break keyword is
used to exit the switch statement and prevent execution of the remaining cases. The default case is
executed if none of the case values match the expression value.

Programming for the web Page 8


Ternary Operator
Monday, February 27, 2023 7:34 PM

The ternary operator is a shorthand way of writing an if-else statement in JavaScript. It is also known
as the conditional operator, because it evaluates a condition and returns one of two values,
depending on whether the condition is true or false.

The ternary operator has three parts:

The condition to be evaluated, followed by a question mark: condition ?

The expression to be returned if the condition is true, followed by a colon: trueExpression :

The expression to be returned if the condition is false: falseExpression

The ternary operator can be useful for writing more concise and readable code, especially when the
expressions being returned are simple. However, it can become difficult to read if the expressions
are complex, so it is important to use it judiciously.

Programming for the web Page 9


Loops
Monday, February 27, 2023 7:35 PM

for loop:
The for loop is often used when you know how many times you want to repeat a block of code. The
initialization, condition, and iteration expressions are all optional, but the semicolons are required.
Here is an example of a for loop that prints the numbers 1 to 5:
for (let i = 1; i <= 5; i++) {
console.log(i);
}

for/in loop:
The for/in loop is used to iterate over the properties of an object. It is often used to loop through the
keys of an object. Here is an example of a for/in loop that loops through the keys of an object:
const person = {
name: 'John',
age: 30,
gender: 'male'
};

for (let key in person) {


console.log(key); // Output: 'name', 'age', 'gender'
}

while loop:
The while loop is often used when you don't know how many times you want to repeat a block of
code. It continues to execute the code block as long as the condition is true. Here is an example of a
while loop that prints the numbers 1 to 5:

let i = 1;

while (i <= 5) {
console.log(i);
i++;
}

do/while loop:
The do/while loop is similar to the while loop, but it always executes the code block at least once,
even if the condition is false. Here is an example of a do/while loop that prints the numbers 1 to 5:

let i = 1;

do {
console.log(i);
i++;
} while (i <= 5);

Programming for the web Page 10


Break
Monday, February 27, 2023 7:41 PM

In JavaScript, the break statement is used to terminate a loop or switch statement prematurely.
When the break statement is encountered, the program flow immediately exits the loop or switch
statement and continues executing the code that appears after the loop or switch statement.

The break statement can only be used inside loops (for, while, do-while) and switch statements. It
cannot be used outside of these statements.

When the break statement is executed, the program flow immediately exits the loop or switch
statement and continues executing the code that appears after the loop or switch statement.

If there are nested loops or switch statements, the break statement only exits the innermost loop or
switch statement. To exit an outer loop or switch statement, you must use a labeled break
statement

Programming for the web Page 11


Interaction
Monday, February 27, 2023 7:50 PM

alert():
The alert() function takes a single argument, which is the message to display to the user. The
message can be any string value, and can include HTML tags and formatting. However, it's usually
best to keep the message simple and easy to understand, as the dialog box may be displayed on top
of other content and can be disruptive to the user's workflow.

Here's an example of using alert() to display a message to the user:


alert("Your order has been submitted. Thank you for your business!");

prompt():
The prompt() function takes two arguments: the message to display to the user, and an optional
default value for the input box. The message should be a string that asks the user for input, and the
default value should be a string that is pre-populated in the input box.

Here's an example of using prompt() to ask the user for their name:
let name = prompt("What is your name?", "John Doe");

If the user enters their name in the input box and clicks OK, the name variable will be set to the
value they entered. If the user clicks Cancel, the name variable will be set to null.

confirm():
The confirm() function takes a single argument, which is the message to display to the user. The
message should be a string that asks the user to confirm or cancel an action. The dialog box will
display two buttons, one for OK and one for Cancel.

Here's an example of using confirm() to ask the user if they want to delete a file:

let result = confirm("Are you sure you want to delete this file?");
if (result === true) {
// delete the file
} else {
// do nothing
}

If the user clicks OK, the result variable will be set to true, and the code inside the if statement will
be executed. If the user clicks Cancel, the result variable will be set to false, and the code inside the
else statement will be executed.

It's important to note that the prompt() and confirm() functions can be used to gather sensitive
information from the user, such as passwords or credit card numbers. Therefore, it's important to
use these functions with caution and to take appropriate security measures to protect the user's
information.

Programming for the web Page 12


Functions
Monday, February 27, 2023 7:55 PM

Functions are one of the fundamental building blocks in JavaScript. They are reusable blocks of
code that perform a specific task, taking some form of input and returning an output 1.
You can define a function with the function keyword, followed by a name, followed by
parentheses (). The parentheses may include parameter names separated by commas. The code
to be executed by the function is written between curly brackets { }

• Functions can have parameters that are variables that receive values when the
function is called. You can use these parameters inside the function body as normal
variables1.
• Functions can have a return statement that specifies what value the function should
give back to the caller. If there is no return statement, the function returns
undefined2.
• Functions can be assigned to variables or passed as arguments to other
functions. This is because functions are first-class objects in JavaScript, meaning
they can have properties and methods just like any other object 2

Programming for the web Page 13


Execution Interval Methods
Monday, February 27, 2023 8:03 PM

setTimeout() and setInterval() are methods that allow you to execute a function after a certain
amount of time or repeatedly at a certain interval. They are useful for creating timers, animations, or
other asynchronous tasks1.

setTimeout() takes a function and a delay (in milliseconds) as arguments and returns a timer ID. It
runs the function once after the delay has passed1. For example:
function sayHello() {
console.log("Hello!");
}

var timer = setTimeout(sayHello, 3000); // sayHello will run after 3 seconds


setInterval() takes a function and an interval (in milliseconds) as arguments and returns a timer ID. It
runs the function repeatedly, starting after the interval has passed, then repeating continuously at
that interval1. For example:
function sayHello() {
console.log("Hello!");
}

var timer = setInterval(sayHello, 1000); // sayHello will run every second

Both methods are asynchronous, meaning they do not block the execution of other code while
waiting for the timer to expire.
Both methods return a timer ID that can be used to cancel the timer with clearTimeout() or
clearInterval().
Both methods accept an optional third argument that can be used to pass additional values to the
function. For example:
function sayHello(name) {
console.log("Hello, " + name + "!");
}

setTimeout(sayHello, 2000, "Alice"); // sayHello will run after 2 seconds with "Alice" as an argument
The actual delay of setTimeout() and setInterval() may vary depending on the browser, system load,
or other factors. They are not guaranteed to be precise.
The function passed to setTimeout() or setInterval() runs in a separate execution context than the
function that called it. This means that the this keyword inside the function may have a different
value than expected.

Programming for the web Page 14

You might also like