JavaScript2 New
JavaScript2 New
Object orientation
What is a Class?
You can think of a class like a blueprint of a house. A class is not a real world object but we
can create objects from a class. It is like an template for an object.
We can create classes using the class keyword which is reserved keyword in JavaScript.
Classes can have their own properties and methods. We will study how to create a class in
detail shortly. This is just a high level overview of a class.
What is an Object?
An object is an instance of a class. Now with the help of the house class we can construct a
house. We can construct multiple houses with the help of same house class.
Consider a Student class. Student can have properties like name, age, standard and so on, and
functions like study, play, and do home work.
class Student{
// Data (Properties)
Name;
Age;
Standard;
//Method (Action)
study(){
}
Play(){
}
doHomeWork(){
}
}
This will keep code that's outside the class from accidentally manipulating internal methods
and properties.
For ex:
class User{
email
password
login(providedPassword){
// Login User
}
checkMessage(){
// Check any new message
}
}
------------------------------------------Child class: Admin class--------------
// Not actual JavaScript syntax
class Admin inherit User{
// email is Inherited Property
// password is Inherited Property
permissions // Own Property
// Inherited Method
login(providedPassword){
// Different Login User
}
// Inherited Method
checkMessage(){
// Check any new message
}
// Own Method
chechStats(){
// Check Stats
}
}
The Login method in Admin is different from the inherited class User (Method overriding).
</script>
</body>
</html>
Or
For ex:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript" src=" myScript.js ">
</script>
</body>
</html>
Language Basics:
Identifier form:
begin with a letter or underscore, followed by any number of letters, underscores, and digits
Case sensitive
Primitives:
JavaScript is a dynamically typed language. It means that a variable doesn’t associate with a
type. In other words, a variable can hold a value of different types.
For example:
let counter=120;// counter is a number
counter=false;//counter is now boolean
counter=”welcome”;//counter is now a string
The typeof null returns object is a known bug in JavaScript. A proposal to fix this was
proposed but rejected. The reason was the that fix would break a lot of existing sites.
JavaScript defines that null is equal to undefined as follows:
console.log(null==undefined);//true
To represent a floating-point number, you include a decimal point followed by at least one
number.
For example:
let price=99.12;
let discount=0.5;
Note: JavaScript automatically converts a floating-point number into an integer number if the
number appears to be a whole number.
The reason is that Javascript always wants to use less memory since a floating-point value
uses twice as much memory as an integer value.
NaN
NaN stands for Not a Number. It is a special numeric value that indicates an invalid number.
For example, the division of a string by a number returns NaN.
For ex:
console.log(‘a’/2);
// Function calling
for (const i of func()) {
obj = obj + i;
}
// Output
console.log(obj);
Output:
123 – Geeks
The yield* expression in JavaScript is used when one wants to delegate some other iterable
object. This function iterates over the particular operand and yields each value that is returned
by it.
Syntax:
yield* expression;
Return Value: It returns the iterable object.
For ex:
function* func1() {
yield "a";
yield "b";
yield* func3();
}
function* func3() {
yield "geeks";
}
function* func2() {
yield* func1();
yield 4/2;
yield 5/2;
}
const it = func2();
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());
Output:
{value: 'a', done: false}
{value: 'b', done: false}
{value: 'geeks', done: false}
{value: 2, done: false}
{value: 2.5, done: false}
{value: undefined, done: true}
</script>
</body>
</html>
For ex:
<DOCTYPE html>
<html>
<head>
<title>
Confirm
</title>
</head>
<body>
<script type="text/javascript">
var c = "Hey";
confirm(c);
</script>
</body>
</html>
2. prompt("What is your name?", "");
Opens a dialog box and displays its string parameter, along with a text
box and two buttons, OK and Cancel
Syntax: prompt();
The second parameter is for a default response if the user
presses OK without typing a response in the text box (waits for OK)
For ex:
<DOCTYPE html>
<html>
<head>
<title>
Prompt
</title>
</head>
<body>
<script type="text/javascript">
var a = prompt("What is the value of 'a'? \n", "");
</script>
</body>
</html>
if (condition) {
// block of code
// execute this if condition is true
}
else {
// block of code
// execute this if condition is false
}
The if...else statement checks the condition and executes code in two ways:
If condition is true, the code inside if is executed. And, the code inside else is
skipped.
If condition is false, the code inside if is skipped. Instead, the code inside else is
executed.
Example :
else {
// if no condition matches
else {
// else body
}
if (rating <= 2) {
console.log("Bad rating");
}
else if (rating >= 4) {
console.log("Good rating!");
}
else {
console.log("Average rating");
}
else {
console.log("Failed");
}
// Output: Passed
1 is printed.
st
1 i=1 true i is increased to
2.
2 is printed.
2nd i=2 true
i is increased to 3.
3 is printed.
3rd i=3 true
i is increased to 4.
4 is printed.
4th i=4 true
i is increased to 5.
5 is printed.
5th i=5 true
i is increased to 6.
The while loop repeatedly executes a block of code as long as a specified condition is true.
The syntax of the while loop is:
while (condition) {
// body of loop
}
Here is how the above program works in each iteration of the loop:
Condition: i <
Variable Action
4
The do...while loop executes a block of code once, then repeatedly executes it as long as the
specified condition is true.
The syntax of the do...while loop is:
do {
// body of loop
} while(condition);
let i = 3;
// do...while loop
do {
console.log(i);
i--;
} while (i > 0);
Output
3
2
1
The break statement terminates the loop immediately when it's encountered
if (i == 3) {
break;
}
console.log(i);
}
Run Code
Output
1
2
// Output:
// 1
// 3
// 5
switch (expression)
{
case value1:
// code block to be executed if expression matches value1
break;
case value2:
// code block to be executed if expression matches value2
break;
...
default:
// code block to be executed if expression doesn't match any case
}
let day = 3;
let activity;
switch (day) {
case 1:
console.log("Sunday");
break;
case 2:
console.log("Monday");
break;
case 3:
console.log("Tuesday");
break;
case 4:
console.log("Wednesday");
break;
case 5:
console.log("Thursday");
break;
case 6:
console.log("Friday");
break;
case 7:
console.log("Saturday");
break;
default:
console.log("Invalid Day");
}
Output
Tuesday
while(condition){ do{
Syntax // loop body // loop body
} } while (condition);
JavaScript Objects:
JavaScript object is a variable that can store multiple data in key-value pair.
const dog = {
name: "Rocky",
};
// access property
console.log(dog.name);
JavaScript Array
Arrays allow us to organize related data by grouping them within a single variable.
Suppose you want to store a list of fruits. Using only variables, this process might look like
this:
We can create an array by placing elements inside an array literal [], separated by commas.
For example,
const numbers = [10, 30, 40, 60, 80];
Here,
In JavaScript, date and time are represented by the Date object. The Date object provides the
date and time information and also provides various methods.
new Date()
new Date(milliseconds)
You can create a date object using the new Date() constructor. For example,
const timeNow = new Date();
console.log(timeNow); // shows current date and time
Output
Mon Jul 06 2020
Here, new Date() creates a new date object with the current date and local time.
Method Description
getDate() Gets the day of the month (1–31) according to local time
getDay() Gets the day of the week (0-6) according to local time
getUTCDate() Gets the day of the month (1–31) according to universal time