Java Script Part1 PPT-Unit2 MSD
Java Script Part1 PPT-Unit2 MSD
<html><head>
<script src="Demo.js"></script>
</head>
<body></body></html>
NOTE: In external file, JavaScript code
is not written inside <script>
</script> tag.
Environmental Setup - Internal
pi = 3.141592;
/* This will throw an error because
the assignment to a const needs
to be done at the time of
declaration and it cannot be re-
initialized. */
console.log("The value of Pi is:
Var
The identifiers declared to hold data
that vary are called 'Variables' and
to declare a variable, the 'var'
keyword is optionally used.
Once the value is initialized, it can be
modified any number of times later in
the program.
It takes the Function scope i.e., it is
globally available to the Function
within which it has been declared and
it is possible to declare the identifier
name a second time in the
same function.
Example
var name = "William";
console.log("Welcome to JS course, Mr." +
name);
var name = "Goth";
// its value is 0
In JavaScript, any other value that
does not belong to the above-
mentioned types is not considered as a
legal number.
Such values are represented
as NaN (Not-a-Number).
Example:
let result = 0/0; // its value is NaN
let result = "Ten" * 5; //its value is NaN
String
When a variable is used to store
textual value.
String values are written in
quotes, either single or double.
Example:
const bigintvar =
674234782346898878947474723894778236
47n; OR
const bigintvar =
BigInt("674234782346898878947474723894
77823647");
const bigintFromNumber = BigInt(10);
common math operations can be
done on BigInt as regular numbers.
But BigInt and regular numbers
cannot be mixed in the expression.
Example:
alert(3n + 2n); // 5
alert(7n / 2n); // 3
alert(8n + 2); // Error: Cannot mix
BigInt and other types
Here the division returns the result
rounded towards zero, without the
decimal part. Thus, all operations on
BigInt return BigInt.
BigInt and regular numbers must
be explicitly converted using
either BigInt() or Number().
Example:
let bigintvar = 6n;
let numvar = 3;
// number to bigint
alert(bigintvar + BigInt(numvar)); // 9
// bigint to number
alert(Number(bigintvar ) + numvar); // 9
In the above example, if the bigintvar is
too large that it won’t fit the number
type, then extra bits will be cut off.
Talking about comparison and
boolean operations on BigInt, it
works fine.
Example:
alert( 8n > 2n ); // true
alert( 4n > 2 ); // true
Even though numbers and BigInts
belong to different types, they can
be equal ==, but not strictly
equal ===.
Example:
alert( 5 == 5n ); // true
alert( 5 === 5n ); // false
When inside if or other boolean
operations, BigInts behave like numbers.
Example:
if (0n) { // never executes}
BigInt 0n is falsy, other values are
considered to be truthy.
Boolean operators, such as ||, && and
others also work perfectly with Bigint’s
similar to numbers.
Example:
alert( 1n || 2 ); // 1, here 1n is considered
truthy
alert( 0n || 2 ); // 2, here 0n is considered
falsy
Non-Primitive data types
The data type is said to be non-primitive
if it is a collection of multiple values.
The variables in JavaScript may not
always hold only individual values, there
are times a group of values are
stored inside a variable.
JavaScript gives non-primitive data types
named Object and Array, to implement
this.
Objects
Objects in JavaScript are a collection
of properties and are represented in
the form of [key-value pairs].
The 'key' is a string or a symbol and
should be a legal identifier.
The 'value‘ can be any JavaScript
value like Number, String, Boolean, or
another object.
JavaScript provides the number of
built-in objects as a part of the
language and user-defined JavaScript
objects can be created using object
literals.
Syntax:
{
key1 : value1,
key2 : value2,
key3 : value3
};
Example:
let mySmartPhone ={
name: "iPhone",
brand: "Apple",
platform: "iOS",
price: 50000 };
Array
To store an ordered collection,
which cannot be achieved using
the objects.
There are two ways of creating an
array:
digits =[1,2,3,"four"];
A single array can hold multiple
values of different data types.
SCREEN OUTPUT AND KEYBOARD
INPUT
ALERT BOX:
Alert box is a very frequently useful to send or
write cautionary messages to end user screen.
Alert box is created by alert method of window
object as shown below.
Syntax: window. alert (“message”);
When alert box is popup, the user has to click
ok to continue browsing or to perform any
further operations.
<head>
<script language="JavaScript">
function add( )
{
a=20;
b=40;
c=a+b;
window.alert("This is for addition of 2
no's");
document.write("Result is: "+c);
}
</script>
</head>
<body onload="add( )">
If you press OK, then prints
output as:
Result is 60.
CONFIRM POPUP BOX
This is useful to verify or accept something
from user. It is created by confirm method
of window object as shown below.
Syntax:- window.confirm
(“message?”);
When the confirm box pop’s up, user must
click either ok or cancel buttons to proceed.
If user clicks ok button it returns the
boolean value true. If user clicks cancel
button, it returns the boolean value false.
<head>
<script>
function sub( ) {
a=50; b=45;
c=a-b;
x=window.confirm("Do you want to see subtraction of
numbers");
if(x==true) {
document.write("result is :"+c);
}
else {
document.write("you clicked cancel button");
}
}
</script>
</HEAD>
<BODY onload="sub( )"> To see the o/p in pop up box:
When you click on OK button, it
will display result as: Result is: 5
When you click on Cancel button,
the control is returned to the
browser.
PROMPT POPUP BOX
To accept data from keyboard at runtime. Prompt
box is created by prompt method of window
object.
Operators are
categorized into unary, binary, and
ternary based on the number of
operands on which they operate in
an expression.
Arithmetic Operators
Arithmetic operators are used for
performing arithmetic operations.
let sum = 5 + 3; // sum=8
let difference = 5 – 3; // difference=2
let product = 5 * 3; // product=15
let division = 5/3; // division=1
let mod = 5%3; // mod=2
let value = 5;
value++; // increment by 1, value=6
let value = 10;
value--; // decrement by 1, value=9
Arithmetic Operators –
String Type Operands
en its radius
if (condition) {
// block of code that will be
executed, if the condition is true
}
Example: