Академический Документы
Профессиональный Документы
Культура Документы
Where HTML and CSS are languages that give structure and style to web
pages, JavaScript gives web pages interactive elements that engage a user.
JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in
1997.
ECMA-262 is the official name of the standard. ECMAScript is the official name of the
language.
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
<p id="demo"></p>
</body>
</html>
Output
My First JavaScript
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Output
<!DOCTYPE html>
<html>
<body>
<p>In this case JavaScript changes the value of the src (source) attribute of an image.</p>
</body>
</html>
Output
In this case JavaScript changes the value of the src (source) attribute of an image.
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').style.fontSize='35px'">Click Me!</button>
</body>
</html>
Output
• What Can JavaScript Do?
• JavaScript can change the style of an HTML element.
• Click Me!
• After click me
• What Can JavaScript Do?
• JavaScript can change the style of an HTML element.
• Click Me!
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').style.display='none'">Click Me!</button>
</body>
</html>
Output
JavaScript is a prototype based object oriented language, which means it doesn't have classes
rather it define behaviors using constructor function and then reuse it using the prototype. no
classes in JavaScript we have only Object.
There are certain features or mechanisms which makes a Language Object Oriented like:
Object
Classes
Encapsulation
Inheritance
Classes– Classes are blueprint of an Object. A class can have many Object, because class is
a template while Object are instances of the class or the concrete implementation.
Before we move further into implementation, we should know unlike other Object Oriented
Language there is no classes in JavaScript we have only Object. To be more precise, JavaScript
is a prototype based object oriented language, which means it doesn’t have classes rather it
define behaviors using constructor function and then reuse it using the prototype.
Encapsulation – The process of wrapping property and function within a single unit is
known as encapsulation.Sometimes encapsulation refers to hiding of data or data
Abstraction which means representing essential features hiding the background detail. Most of
the OOP languages provide access modifiers to restrict the scope of a variable, but there are no
such access modifiers in JavaScript but there are certain way by which we can restrict the scope
of variable within the Class/Object.
Inheritance – It is a concept in which some property and methods of an Object is being used by
another Object. Unlike most of the OOP languages where classes inherit classes, JavaScript
Object inherits Object i.e. certain features (property and methods)of one object can be reused by
other Objects.
• 4.The JavaScript can be indirectly embedded in an XHTML document using the src
attribute of a <script> tag, whose value is name of a file that contains the script.
• </script>
• Closing tag is required even if script element has src attribute included.
2)It also avoids the problem of hiding scripts from older browsers.
3)It is good to separate the computation provided by JavaScript from the layout and
presentation provided by XHTML and CSS respectively.
1. Must begin with (-), or a letter. Subsequent characters may be letters,underscores or digits.
3. Case sensitive
4. No uppercase letters
All primitive values have one of the five primitive types:
• Number,
• String,
• Boolean,
• Undefined, or Null.
• There are 6 primitive data types: string, number, bigint, boolean, undefined, and symbol.
...
• to any object)
• occurrence of a variable
• declared
• var sum = 0,
• today = "Monday",
• flag = false;
Expressions
• An expression is any valid set of literals, variables, operators, and expressions that
evaluates to a single value.
• The value may be a number, a string, or a logical value. Conceptually, there are two types
of expressions: those that assign a value to a variable, and those that simply have a value.
For example, the expression
• On the other hand, the expression 3 + 4 simply evaluates to 7; it does not perform an
assignment. The operators used in such expressions are referred to simply as operators.
• The special keyword null denotes a null value. In contrast, variables that have not been
assigned a value are undefined, and cannot be used without a run-time error.
Conditional expressions
• A conditional expression can have one of two values based on a condition. The syntax is
• For example,
• status = (age >= 18) ? "adult" : "minor" This statement assigns the value "adult" to the
variable status if age is eighteen or greater. Otherwise, it assigns the value "minor" to
status.
Operators
• Arithmetic Operators
• Comparison Operators
• Assignment Operators
Assignment operators:
• An assignment operator assigns a value to its left operand based on the value of its right
operand. The basic assignment operator is equal (=), which assigns the value of its right
operand to its left operand. That is, x = y assigns the value of y to x.
• The other operators are shorthand for standard arithmetic operations as follows:
• x += y means x = x + y
• x |= means x = x | y
Arithmetic operators
• Arithmetic operators take numerical values (either literals or variables) as their operands
and return a single numerical value.
• The standard arthmetic operators are addition (+), subtraction (-), multiplication (*), and
division (/). These operators work in the standard way
• Modulus (%)
The modulus operator returns the first operand modulo the second operand, that
is, var1 modulo var2, in the statement above, where var1 and var2 are variables. The
modulo function is the remainder of integrally dividing var1 by var2. For example, 12 %
5 returns 2
• This operator increments (adds one to) its operand and returns a value. If used postfix,
with operator after operand (for example x++), then it returns the value before
incrementing. If used prefix with operator before operand (for example, ++x), then it
returns the value after incrementing.
• y = x++
• y = ++x
• var-- or --var
• This operator decrements (subtracts one from) its operand and returns a value. If used
postfix (for example x--) then it returns the value before decrementing. If used prefix (for
example, --x), then it returns the value after decrementing.
• y = x--
• y = --x
• The operands are converted to 32-bit integers, and expressed a series of bits (zeros and
ones).
• Each bit in the first operand is paired with the corresponding bit in the second operand:
first bit to first bit, second bit to second bit, and so on.
• The operator is applied to each pair of bits, and the result is constructed bitwise.
• Bitwise XOR ^ returns a one if one but not both operands are one.
A comparison operator
• A comparison operator compares its operands and returns a logical value based on
whether the comparison is true or not.
• When used on string values, the comparisons are based on the standard lexicographical
ordering.
• Not equal (!=): returns true if the operands are not equal.
• Greater than (>): returns true if left operand is greater than right operand. Example: x > y
returns true if x is greater than y.
• Greater than or equal to (>=): returns true if left operand is greater than or equal to right
operand. Example: x >= y returns true if x is greater than or equal to y.
• Less than (<): returns true if left operand is less than right operand. Example: x < y
returns true if x is less than y.
• Less than or equal to (<=): returns true if left operand is less than or equal to right
operand. Example: x <= y returns true if x is less than or equal to y.
String operators
• In addition to the comparison operators, which may be used on string values, the
concatenation operator (+) concatenates two string values together, returning another
string that is the union of the two operand strings. For example,
• "my string"
• The shorthand assignment operator += can also be used to concatenate strings. For
example, if the variable mystring is a string that has the value "alpha", then the
expression
• mystring += "bet“
Operator Precedence
• The precedence of operators determines the order they are applied when evaluating an
expression. You can override operator precedence by using parentheses.
• comma ,
• assignment = += -= *= /= %= <<= >>= >>>= &= ^= |=
• conditional ?:
• logical-or ||
• logical-and &&
• bitwise-or |
• bitwise-xor ^
• bitwise-and &
• equality == !=
• addition/subtraction + -
• multiply/divide * / %
• negation/increment ! ~ - ++ --
• call, member () [] .
• using innerHTML.
• using document.write().
• using window.alert().
• using console.log().
Using inner.HTML
• To access an HTML element, JavaScript can use
the document.getElementById(id) method.
• <!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
KEYBOARD INPUT
• keydown
• keypress
• keyup
• Conditional statements are used to perform different actions based on different conditions
• Use else if to specify a new condition to test, if the first condition is false
The if statement
Syntax
• if (condition) {
// block of code to be executed if the condition is true
}
• Note that if is in lowercase letters. Uppercase letters (If or IF) will generate a JavaScript
error.
Example
• if (hour < 18) {
greeting = "Good day";
}
• 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
}
Syntax
• 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
}
Syntax
• switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
• The value of the expression is compared with the values of each case.
EXAMPLE
• switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
LOOPS
Syntax
• while (condition) {
// code block to be executed
}
• <!DOCTYPE html>
• <html>
• <body>
• <h2>JavaScript While Loop</h2>
• <p id="demo"></p>
• <script>
• var i = 0;
• i++;
• }
• document.getElementById("demo").innerHTML = text;
• </script>
• </body>
• </html>
• The do/while loop is a variant of the while loop. This loop will execute the code block
once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true.
Syntax
• do {
// code block to be executed
}
while (condition);
Example
• The example below uses a do/while loop. The loop will always be executed at least once,
even if the condition is false, because the code block is executed before the condition is
tested:
• The example below uses a do/while loop. The loop will always be executed at least once,
even if the condition is false, because the code block is executed before the condition is
tested:
• Example
• do {
text += "The number is " + i;
i++;
}
while (i < 10);