Unit 4 Javascript
Unit 4 Javascript
JAVASCRIPT
• Internal JS: We can add JavaScript directly to our HTML file by writing the code
inside the <script> tag. The <script> tag can either be placed inside the <head> or
the <body> tag according to the requirement.
• External JS: We can write JavaScript code in another files having an extension.js
and then link this file inside the <head> tag of the HTML file in which we want to
add this code.
Syntax:
<script>
// JavaScript Code
</script>
Example:
History of JavaScript
It was created by Brendan Eich in 1995. He was an engineer at Netscape. It was originally
going to be named LiveScript but was renamed. Unlike most programming languages,
JavaScript language has no concept of input or output. It is designed to run as a scripting
language in a host environment, and it is up to the host environment to provide
mechanisms for communicating with the outside world. The most common host
environment is the browser.
Features of JavaScript
According to a recent survey conducted by Stack Overflow, JavaScript is the most
popular language on earth. With advances in browser technology and JavaScript having
moved into the server with Node.js and other frameworks, JavaScript is capable of so
much more. Here are a few things that we can do with JavaScript:
• JavaScript was created in the first place for DOM manipulation. Earlier websites
were mostly static, after JS was created dynamic Web sites were made.
• Functions in JS are objects. They may have properties and methods just like other
objects. They can be passed as arguments in other functions.
• Performs Form Validation although the forms are created using HTML.
• No compiler is needed.
Applications of JavaScript
• Web Development: Adding interactivity and behavior to static sites JavaScript was
invented to do this in 1995. By using AngularJS that can be achieved so easily.
• Web Applications: With technology, browsers have improved to the extent that a
language was required to create robust web applications. When we explore a map
in Google Maps then we only need to click and drag the mouse. All detailed view
is just a click away, and this is possible only because of JavaScript. It uses
Application Programming Interfaces(APIs) that provide extra power to the code.
The Electron and React are helpful in this department.
• Server Applications: With the help of Node.js, JavaScript made its way from client
to server and Node.js is the most powerful on the server side.
• Games: Not only in websites, but JavaScript also helps in creating games for
leisure. The combination of JavaScript and HTML 5 makes JavaScript popular in
game development as well. It provides the EaseJS library which provides solutions
for working with rich graphics.
• Art: Artists and designers can create whatever they want using JavaScript to draw
on HTML 5 canvas, and make the sound more effective also can be
used p5.js library.
• Machine Learning: This JavaScript ml5.js library can be used in web development
by using machine learning.
• Mobile Applications: JavaScript can also be used to build an application for non-
web contexts. The features and uses of JavaScript make it a powerful tool for
creating mobile applications. This is a Framework for building web and mobile
apps using JavaScript. Using React Native, we can build mobile applications for
different operating systems. We do not require to write code for different systems.
Write once use it anywhere!
A lightweight language does not consume much of your CPU’s resources. It doesn’t put
excess strain on your CPU or RAM. JavaScript runs in the browser even though it has
complex paradigms and logic which means it uses fewer resources than other languages.
For example, NodeJs, a variation of JavaScript not only performs faster computations but
also uses fewer resources than its counterparts such as Dart or Java.
Example:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Pros: Simple for small scripts or testing code. Cons: Can make HTML harder to read and
manage, especially for larger scripts.
2. Internal JavaScript
You can place JavaScript within the <script> tags inside the <head> or <body> section
of your HTML file.
Example :
<!DOCTYPE html>
<html>
<head>
<script>
</script>
</head>
<body>
</body>
</html>
Pros: Keeps the JavaScript in one place within the HTML file. Cons: Still not ideal for large
scripts, as it can make the HTML file longer and harder to read.
3. External JavaScript
The best practice is often to keep JavaScript in an external file. This keeps the HTML
and JavaScript separate, making the code easier to manage, especially as it grows.
1. Create an external JavaScript file, typically with the .js extension, such as
script.js.
Example:
// script.js
function showMessage() {
2. Link the external JavaScript file in your HTML file using a <script> tag with the
src attribute.
Example:
<!DOCTYPE html>
<html>
<head>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
Pros: Keeps HTML and JavaScript separate, ideal for reusability, and makes code easier
to read and maintain. Cons: Requires managing separate files, but this is usually
beneficial in the long run.
• If the <script> tag is placed in the <head>, it might delay the loading of the page
content. To avoid this, you can use the defer attribute, which tells the browser to
load the JavaScript only after the HTML document is completely parsed:
JavaScript Variables
In a programming language, variables are used to store data values.
JavaScript uses the keywords var, let and const to declare variables.
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x is defined as a variable. Then, x is assigned the value of 6:</p>
<p id="demo"></p>
<script>
let x;
x = 6;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Output: 6
4.4 JavaScript Comments
JavaScript comments can be used to explain JavaScript code, and to make it more
readable.
JavaScript comments can also be used to prevent execution, when testing alternative
code.
Any text between // and the end of the line will be ignored by JavaScript (will
not be executed).
<html>
<body>
<h1 id="myH"></h1>
<p id="myP"></p>
<script>
// Change heading:
// Change paragraph:
</script>
</body>
</html>
Output:
JavaScript Comments
My first paragraph.
Multi-line Comments
Multi-line comments start with /* and end with */.
This example uses a multi-line comment (a comment block) to explain the code:
<!DOCTYPE html>
<html>
<body>
<h1 id="myH"></h1>
<p id="myP"></p>
<script>
/*
*/
</script>
</body>
</html>
Output:
JavaScript Comments
My first paragraph.
Note:
It is most common to use single line comments.
Block comments are often used for formal documentation.
Using Comments to Prevent Execution
Using comments to prevent execution of code is suitable for code testing.
Adding // in front of a code line changes the code lines from an executable line
to a comment.
<html>
<body>
<h2>JavaScript Comments</h2>
<h1 id="myH"></h1>
<p id="myP"></p>
<script>
</script>
</body>
</html>
Output:
JavaScript Comments
My first paragraph.
• Automatically
• Using var
• Using let
• Using const
<html>
<body>
<h1>JavaScript Variables</h1>
<p id="demo"></p>
<script>
x = 5;
y = 6;
z = x + y;
</script>
</body>
</html>
Output:
JavaScript Variables
In this example, x, y, and z are undeclared.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p id="demo"></p>
<script>
var x = 5;
var y = 6;
var z = x + y;
</script>
</body>
</html>
Output:
JavaScript Variables
In this example, x, y, and z are variables.
3. Always use const if the type should not be changed (Arrays and Objects)
JavaScript Identifiers
All JavaScript variables must be identified with unique names.
Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).
The general rules for constructing names for variables (unique identifiers) are:
Note:
JavaScript identifiers are case-sensitive.
• String
• Number
• Bigint
• Boolean
• Undefined
• Null
• Symbol
• Object
objects, arrays, dates, maps, sets, intarrays, floatarrays, promises, and more.
Example:
// Numbers:
let length = 16;
let weight = 7.5;
// Strings:
let color = "Yellow";
let lastName = "Johnson";
// Booleans
let x = true;
let y = false;
// Object:
const person = {firstName:"John", lastName:"Doe"};
// Array object:
const cars = ["Saab", "Volvo", "BMW"];
// Date object:
const date = new Date("2022-03-25");
Note:
A JavaScript variable can hold any type of data.
The Concept of Data Types
In programming, data types is an important concept.
let x = 16 + "Volvo";
Does it make any sense to add "Volvo" to sixteen? Will it produce an error or will it
produce a result?
Note:
When adding a number and a string, JavaScript will treat the number as a string.
Example 1:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p>When adding a number and a string, JavaScript will treat the number
as a string.</p>
<p id="demo"></p>
<script>
let x = 16 + "Volvo";
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Output:
JavaScript
When adding a number and a string, JavaScript will treat the number as a string.
16Volvo
Example 2:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p> When adding a string and a number, JavaScript will treat the number as a
string.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Output:
JavaScript
When adding a string and a number, JavaScript will treat the number as a string.
Volvo16
JavaScript evaluates expressions from left to right. Different sequences can produce different
results:
Example 1:
In the first example, JavaScript treats 16 and 4 as numbers, until it reaches "Volvo".
In the second example, since the first operand is a string, all operands are treated as
strings.
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:
Example:
<!DOCTYPE html >
<html>
<body>
<h2>JavaScript Strings</h2>
<p>Strings are written with quotes. You can use single or double quotes:</p>
<p id="demo"></p>
<script>
</script>
</body>
</html>
Output:
JavaScript Strings
Strings are written with quotes. You can use single or double quotes:
Volvo XC60
Volvo XC60
You can use quotes inside a string, as long as they don't match the quotes surrounding
the string:
Example:
JavaScript Numbers
All JavaScript numbers are stored as decimal numbers (floating point).
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Numbers</h2>
<p id="demo"></p>
<script>
</script>
</body>
</html>
Output:
JavaScript Numbers
Numbers can be written with, or without decimals:
34
34
3.14
Exponential Notation
Extra large or extra small numbers can be written with scientific (exponential) notation:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Numbers</h2>
<p id="demo"></p>
<script>
let y = 123e5;
let z = 123e-5;
document.getElementById("demo").innerHTML = y + "<br>" + z;
</script>
</body>
</html>
Output:
JavaScript Numbers
Extra large or extra small numbers can be written with scientific (exponential) notation:
12300000
0.00123
Note:
Most programming languages have many number types:
Whole numbers (integers):
- byte (8-bit), short (16-bit), int (32-bit), long (64-bit)
Real numbers (floating-point):
-float (32-bit), double (64-bit).
Javascript numbers are always one type:
-double (64-bit floating point).
JavaScript BigInt
All JavaScript numbers are stored in a 64-bit floating-point format.
JavaScript BigInt is a new datatype (ES2020) that can be used to store integer values that
are too big to be represented by a normal JavaScript Number.
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Numbers</h2>
<p id="demo"></p>
<p>You cannot perform math between a BigInt type and a Number type.</p>
<script>
let x = BigInt("123456789012345678901234567890");
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Output:
JavScript Bigint
A BigInt can not have decimals.
123456789012345678901234567890
You cannot perform math between a BigInt type and a Number type.
JavaScript Booleans
Booleans can only have two values: true or false.
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Numbers</h2>
<p id="demo"></p>
<script>
</script>
</body>
</html>
Output:
JavaScript Booleans
Booleans can have two values: true or false:
true
false
The following code declares (creates) an array called cars, containing three items (car
names):
Example:
<!DOCTYPE html>
<html>
<body>
<p> Array indexes are zero-based, which means the first item is [0].</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = cars[0];
</script>
</body>
</html>
Output:
JavaScript Arrays
Array indexes are zero-based, which means the first item is [0].
Saab
- Array indexes are zero-based, which means the first item is [0], second is [1], and so on.
JavaScript Objects
JavaScript objects are written with curly braces {}.
Example:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
const person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
document.getElementById("demo").innerHTML =
</body>
</html>
Output:
JavaScript Objects
John is 50 years old.
- The object (person) in the example above has 4 properties: firstName, lastName, age,
and eyeColor.
Undefined
In JavaScript, a variable without a value, has the value undefined.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<p id="demo"></p>
<script>
let car;
</script>
</body>
</html>
Output:
JavaScript Operators
The typeof Operator
The value of a variable with no value is undefined.
Undefined
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<p id="demo"></p>
<script>
car = undefined;
document.getElementById("demo").innerHTML =
</script>
</body>
</html>
Output:
JavaScript Operators
The typeof Operator
Variables can be emptied if you set the value to undefined.
Example 1:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
</script>
</body>
</html>
Output:
JavaScript Operators
The typeof Operator
The typeof operator returns the type of a variable or an expression.
string
string
string
Example 2:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
typeof 0 + "<br>" +
typeof (3 + 4);
</script>
</body>
</html>
Output:
JavaScript Operators
The typeof Operator
The typeof operator returns the type of a variable or an expression.
number
number
number
number
number
Empty Values
An empty value has nothing to do with undefined.
Example:
<!DOCTYPE html>
<html>
<body>
<script>
document.getElementById("demo").innerHTML =
"The value is: " + car + "<br>" + "The type is: " + typeof car;
</script>
</body>
</html>
Output:
JavaScript Datatypes
An empty string has both a legal value and a type:
Examples:
JavaScript Assignment
The Assignment Operator (=) assigns a value to a variable:
Example 1:
let x = 10;
Example 2:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x + y;
// Display z
</script>
</body>
</html>
Output:
JavaScript Operators
The Assignment (=) Operator
The sum of x + y is: 7
JavaScript Addition
The Addition Operator (+) adds numbers:
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The + Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
The + Operator
7
JavaScript Multiplication
The Multiplication Operator (*) multiplies numbers:
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The * Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
let y = 2;
let z = x * y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
The * Operator
10
• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• String Operators
• Logical Operators
• Bitwise Operators
• Ternary Operators
• Type Operators
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
% Modulus (Remainder)
++ Increment
-- Decrement
Arithmetic Operations
A typical arithmetic operation operates on two numbers.
Example 1:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<p>A typical arithmetic operation takes two numbers and produces a new
number.</p>
<p id="demo"></p>
<script>
let x = 100 + 50;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
A typical arithmetic operation takes two numbers and produces a new number.
150
or variables:
Example 2:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<p>A typical arithmetic operation takes two numbers (or variables) and
produces a new number.</p>
<p id="demo"></p>
<script>
let a = 100;
let b = 50;
let x = a + b;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
A typical arithmetic operation takes two numbers (or variables) and produces a
new number.
150
or expressions:
Example 3:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<p>A typical arithmetic operation takes two numbers (or expressions) and
produces a new number.</p>
<p id="demo"></p>
<script>
let a = 3;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
A typical arithmetic operation takes two numbers (or expressions) and produces
a new number.
450
Operators and Operands
The numbers (in an arithmetic operation) are called operands.
The operation (to be performed between the two operands) is defined by an operator.
100 + 50
Adding
The addition operator (+) adds numbers:
Example:
let x = 5;
let y = 2;
let z = x + y;
Subtracting
The subtraction operator (-) subtracts numbers.
Example:
let x = 5;
let y = 2;
let z = x - y;
Multiplying
Example:
let x = 5;
let y = 2;
let z = x * y;
Dividing
The division operator (/) divides numbers.
Example:
let x = 5;
let y = 2;
let z = x / y;
Remainder
The modulus operator (%) returns the division remainder.
Example:
let x = 5;
let y = 2;
let z = x % y;
Incrementing
The increment operator (++) increments numbers.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<p>The ++ Operator</p>
<p id="demo"></p>
<script>
let x = 5;
x++;
let z = x;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
The ++ Operator
Decrementing
The decrement operator (--) decrements numbers.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<p>The -- Operator</p>
<p id="demo"></p>
<script>
let x = 5;
x--;
let z = x;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
The -- Operator
Exponentiation
The exponentiation operator (**) raises the first operand to the power of the second
operand.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<p>The ** Operator</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = x ** 2;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
The ** Operator
25
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<p> Math.pow()</p>
<p id="demo"></p>
<script>
let x = 5;
document.getElementById("demo").innerHTML = Math.pow(x,2);
</script>
</body>
</html>
Output:
JavaScript Arithmetic
Math.pow()
25
Operator Precedence
Operator precedence describes the order in which operations are performed in an
arithmetic expression.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 100 + 50 * 3;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
Operator Precedence
250
Is the result of example above the same as 150 * 3, or is it the same as 100 + 150?
Is the addition or the multiplication done first?
As in traditional school mathematics, the multiplication is done first.
Multiplication (*) and division (/) have higher precedence than addition (+) and
subtraction (-).
And (as in school mathematics) the precedence can be changed by using parentheses.
When using parentheses, the operations inside the parentheses are computed first:
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Operator Precedence</h2>
<p id="demo"></p>
<script>
</script>
</body>
</html>
Output:
JavaScript Arithmetic
Operator Precedence
Multiplication has precedence over addition.
450
When many operations have the same precedence (like addition and subtraction or
multiplication and division), they are computed from left to right:
Example 1:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Operator Precedence</h2>
<p> When many operations has the same precedence, they are computed from
left to right.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 100 + 50 - 3;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
Operator Precedence
When many operations has the same precedence, they are computed from left to
right.
147
OR
Example 2:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>Operator Precedence</h2>
<p> When many operations has the same precedence, they are computed from
left to right.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 100 / 50 * 3;
</script>
</body>
</html>
Output:
JavaScript Arithmetic
Operator Precedence
When many operations has the same precedence, they are computed from left to
right.
6
4.9 JavaScript Assignment Operators
Assignment operators assign values to JavaScript variables.
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
^= x ^= y x=x^y
|= x |= y x=x|y
||= x ||= y x = x || (x = y)
??= x ??= y x = x ?? (x = y)
Note:
The Logical assignment operators are ES2020.
The = Operator
The Simple Assignment Operator assigns a value to a variable.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Simple Assignment</h2>
<h3>The = Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
</script>
</body>
</html>
Output:
JavaScript Assignments
Simple Assignment
The = Operator
Value of x is: 10
The += Operator
The Addition Assignment Operator adds a value to a variable.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h3>The += Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x += 5;
document.getElementById("demo").innerHTML = "Value of x is: " + x;
</script>
</body>
</html>
Output:
JavaScript Assignments
Addition Assignment
The += Operator
Value of x is: 15
The -= Operator
The Subtraction Assignment Operator subtracts a value from a variable.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h3>The -= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x -= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Subtraction Assignment
The -= Operator
Value of x is: 5
The *= Operator
The Multiplication Assignment Operator multiplies a variable.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Multiplication Assignment</h2>
<h3>The *= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x *= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Multiplication Assignment
The *= Operator
Value of x is: 50
The **= Operator
The Exponentiation Assignment Operator raises a variable to the power of the
operand.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<p id="demo"></p>
<script>
let x = 10;
x **= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Exponentiation Assignment
The **= Operator
Value of x is: 100000
The /= Operator
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h3>The /= Operator</h3>
<p id="demo"></p>
<script>
let x = 10;
x /= 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Output:
JavaScript Assignments
Division Assignment
The /= Operator
2
The %= Operator
The Remainder Assignment Operator assigns a remainder to a variable.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<p id="demo"></p>
<script>
let x = 10;
x %= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Remainder Assignment
The %= Operator
Value of x is: 0
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<p id="demo"></p>
<script>
let x = -100;
x <<= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Left Shift Assignment
The <<= Operator
Value of x is: -3200
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<p id="demo"></p>
<script>
let x = -100;
x >>= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Right Shift Assignment
The >>= Operator
Value of x is: -4
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<p id="demo"></p>
<script>
let x = -100;
x >>>= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Right Shift Assignment
The >>>= Operator
Value of x is: 134217724
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<p id="demo"></p>
<script>
let x = 100;
x &= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Bitwise AND Assignment
The &= Operator
Value of x is: 4
The |= Operator
The Bitwise OR Assignment Operator does a bitwise OR operation on two operands
and assigns the result to the variable.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<p id="demo"></p>
<script>
let x = 100;
x |= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Bitwise OR Assignment
The |= Operator
Value of x is: 101
The ^= Operator
The Bitwise XOR Assignment Operator does a bitwise XOR operation on two
operands and assigns the result to the variable.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<p id="demo"></p>
<script>
let x = 100;
x ^= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Bitwise XOR Assignment
The ^= Operator
Value of x is: 97
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<h2>Logical AND Assignment</h2>
<p id="demo"></p>
<script>
let x = 100;
x &&= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Logical AND Assignment
The &&= Operator
If the first value is true, the second value is assigned.
Value of x is: 5
Note:
The &&= operator is an ES2020 feature.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<p id="demo"></p>
<script>
let x = undefined;
x ||= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
Logical OR Assignment
The ||= Operator
If the first value is false, the second value is assigned:
Value of x is: 5
Note:
The ||= operator is an ES2020 feature.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Assignments</h1>
<p> The ??= operator is used between two values. If the first value is undefined or null,
the second value is assigned.</p>
<p id="demo"></p>
<script>
let x;
document.getElementById("demo").innerHTML = x ??= 5;
</script>
</body>
</html>
Output:
JavaScript Assignments
The ??= Operator
The ??= operator is used between two values. If the first value is undefined or
null, the second value is assigned.
5
Note:
The ??= operator is an ES2020 feature.
Comparison Operators
Comparison operators are used in logical statements to determine equality or difference
between variables or values.
== equal to x == 8 false
x == 5 true
x == "5" true
x !== 8 true
Example:
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:
Operator Description Example
|| or (x == 5 || y == 5) is false
Syntax
variablename = (condition) ? value1:value2
Example:
If the variable age is a value below 18, the value of the variable voteable will be "Too
young", otherwise the value of voteable will be "Old enough".
When comparing a string with a number, JavaScript will convert the string to a number
when doing the comparison. An empty string converts to 0. A non-numeric string
converts to NaN which is always false.
Case Value
2 < 12 true
2 == "John" false
When comparing two strings, "2" will be greater than "12", because (alphabetically) 1 is
less than 2.
To secure a proper result, variables should be converted to the proper type before
comparison:
Example:
age = Number(age);
if (isNaN(age)) {
voteable = "Input is not a number";
} else {
voteable = (age < 18) ? "Too young" : "Old enough";
}
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The ?? Operator</h2>
<p>The ?? operator returns the first argument if it is not nullish (null or
undefined). Otherwise it returns the second.</p>
<p id="demo"></p>
<script>
</script>
</body>
</html>
Output:
JavaScript Operators
The ?? Operator
The ?? operator returns the first argument if it is not nullish (null or undefined).
Otherwise it returns the second.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The ?. Operator</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = name;
</script>
</body>
</html>
Output:
JavaScript Operators
The ?. Operator
The ?. operator returns undefined if an object is undefined or null (instead of
throwing an error).
undefine
In this example, Concatenating str1 and str2 using the ‘+’ operator, the result variable
holds the string “GeeksforGeeks”.
Example:
console.log(result);
Output:
GeeksforGeeks