Javascript Notes
Javascript Notes
What is JavaScript?
Client-side:
Server-side:
Used to:
ECMAScript:
institute (“Qspider”);
institute (“Pyspider”)
<Html lang=’en’>
the HTML file using the script
tag
<Head>
*JavaScript engine is a program
</Head>
that is responsible for compiling
<Body> and executing JavaScript
<script src= ‘index.js’> </script> program
Web browser
The interpreter will run line by
line if logical errors return a
runtime error. If there is no
logical error, we can see the
result in a browser.
Output
Ja v a Sc r i p t e n g i n e
Everything works like the client side, but we run outside the browser by
using backend software like NodeJS, ExpressJs, NestJs, and NextJs.
output will be seen in the console
JAVA JAVASCRIPT
Functions
What is the Function? And it’s advantages?
Advantages
And Code modularity (Bigger tasks can be divided into smaller tasks,
each small task can be defined as a function)
Syntax: - function add (par1, par2) {
2. function has a name; function name holds the whole structure of the
Function
3While passing parameters inside a parenthesis don’t use var, let, const
keyword
Types of function
2. Anonymous function
4. Function expression
5. Callback function
6. Self-invoking function
}
Syntax:- function () {}
}
3. Arrow function:
About parenthesis
If function has no parameter and function has more than one parameter
parenthesis are mandatory
If function has more than one statement than curly brackets are
mandatory if function is returning something than return key mandatory
4. function expression:
5. Callback function: -is a function which is passed as argument to
another function is called as callback function
This function gets invoke automatically once in the entire life cycle of
program
Example 1: (function () {
console.log ("hello");
}());
})();
Strings
What is the string?
Examples: -
Note:-
JavaScript will misunderstand the same quotes within the same quoted
string it will give an error.
To use the same quotes within the same quoted string use backslash
before that quote it will consider that quote as a character
You can write quotes within quotes, but they don't have to match with
the enclosed quotes
1. indexOf ()
2.lastIndexOf()
3.search()
4.startsWith()
5.endsWith()
6.includes()
7.slice()
8.substring()
9.substr()
10.charAt()
11.charCodeAt()
12.toUpperCase()
13.toLowerCase()
14.concat()
15.trim()
16.split()
17.replace()
18.replaceAll()
This method accepts a string value, finds that the string value is in
which index in the specified string It returns the index position of the
first occurred match only. If string value is not matching it will return -1.
Accepts:- two arguments
The second argument is the search position from which index you want
to search. The second argument is not mandatory, if you have not given
a second argument it is going to search from 0 th index position.
Example:-
It does not slice the original string. slice() method creates a new string.
The first argument is to specify the start index position for the slice
The second argument is to specify the end index position for the slice.
slice () method does not include the last index character while slicing
Examples:
examples
Method joins two or more strings this method creates a new string it will
not change the original strings
15. string.trim() method removes white space from both the side of the
string
console.log (string3. split ('')); // output ['J',’s’, 'p', 'i',’d’, 'e', 'r',’s’] if we
have given only '' quotes split () method makes every character as an
array element
introduced in ECMAScript-6
scientist who served as the 11th President of India from 2002 to 2007`
// allow us to write multiline code
let strTemplate3 = `
<div class="parent1">
<h1>heading tag</h1>
<p>Paragraph tag</p>
</div>
<div class="parent2">
<h1>heading tag</h1>
<p>Paragraph tag</p>
</div>
let value2 = 65
Numbers
JavaScript has only one type of number, it can be written with or without
a decimal point
The parameter defines the number of characters after the decimal point.
NaN: - Not a Number, whenever we are operating with the number and
character JavaScript, returns NaN
inNaN():-You can use the global JavaScript function isNaN() to find out
if a value is a not a number
Array creation : to create an array use array literal [] and store elements in
between array literal each element separated by comm
let list = [1, 2, 3, { name: "arun", lastName: "sagar", age: 25 }, "sagar", [10, 11, 12],
100, 500] // heterogeneous element (different types of elements)
console.log(list.length);// output : 8
Read ( to read array element use index number, array's first element will be in
0th index position and array's last element will be stored in array.length-1 )
// 0 1 2 3 4
Update
array[0] = "Kalam";
array[5] = 55;
array[6] = 100;
Used:- the pop() method is used to remove the last element from the
array.
Return: pop() method returns the removed element from the array
array.shift()
Returns:- This method returns the removed element from the array.
array.push()
Used: - push() method adds new elements at the end of the array.
array.unshift()
console.log(numbers); //after unshift() output:- [90, 91, 92, 93, 94, 95,
96, 100, 200, 300, 400, 500]
the first argument: where the first argument is the index position from
where you want to add or delete the elements.
From the third argument all the arguments are the new elements added
to the array,
Use: -This method fills the array with new element in a specified index
the second argument is the start position from where you want to fill
the third argument is the end position till where you want to fill
num1.fill() // If you have not passed any arguments this method fills the
whole array with the undefined value.
num2.fill(5) //if you ignore second and third argument then it fills the
new element from 0th index to till the end of an array
num3.fill('ele', 2); // If you have ignored the third argument, it fills the
new element from the start index to till the end of the array
num4.fill('ravi', 1, 3);
array. reverse()
num. reverse();
console.log(num); //output [6, 5, 4, 3, 2, 1]
array.indexOf()
Used:- this method is used to find out the index position of an array
element.
The first argument is an array element & the second argument is the
index position from where you want to search.
If you have not passed the second argument, it will start searching from
the 0th index.
console.log(num.indexOf(5));// output 4
console.log(num.indexOf(100)); // output -1
array.lastIndexOf()
If you don't pass the second argument, it starts checking from the last
index
console.log(num.lastIndexOf(1)); // output 4
console.log(num.lastIndexOf(100)); // output -1
array.includes()
array.join('*')
join() method converts the array into a string and separates the
elements by a separator.
console.log(num.join(" => ")); //output 1 => 2 => 3 => 4 => 5 => 6 => 7 =>
8 => 9
Used to:- this method is used to iterate an array of elements and their
indexes.
// Example 1
num.forEach(callback);
console.log(element);
/* output
12
23
45
56
78
89
*/
// Example
/* output
0 15
1 59
2 75
3 68
4 23
5 65
6 123
*/
// Example3
/* output
1
*/
array.map()
// Example 1
return ele * 2
}
// Example 2
return ele.toUpperCase()
})
array.filter()
// Example 1:-
let num = [12, 45, 78, 89, 56, 32, 15, 59, 50];
})
// Example 2:-
array.some()
Used to: this method checks whether any one element is passing the
condition or not
Returns:- this method takes one call back function that callback function
passes a condition on array element if any element is passing the
condition this method returns true else it returns false
// Example1
let ages = [14, 15, 18, 19, 20, 30, 35, 40, 50];
})
// Example2
array.every()
every() method passes a test on each array element. If all the elements
pass a test, then it returns true. else every() method returns false.
Argument:- this method accepts one argument.
let num = [150, 250, 350, 450, 550, 50, 650, 750];
})
The find() method returns the value of the first array element that
passes a test function.
// Example1:-
console.log(result); // output:- 11
// Example2:-
// Example3:-
Array.isArray()
Used to:- this method is used to check whether the given data is an
array or not
Returns: this method returns true if the given argument is an array else
this method returns false
let employee = {
firstName: "Ravi",
lastName: "Uppar",
company: "varroc",
salary: 450000
}
update
employee['salary'] = 600000;
creation
employee['designation'] = "Developer";
delete
delete employee.company;
delete employee['salary']
let numbers = [100, 200, 300, 400, 500, 600, 700, 800];
console.log(number);
/* output
}
100
200
300
400
500
600
700
800
*/
console.log(color);
} /* output
red
green
yellow
black
blue
purple */
let students = [
]
console.log(student);
}/*output
{sname: 'Ravi', age: 25, rollNo: 1, marks: 620}
*/
console.log(student.name);
} /*output
Ravi
Akash
Kalyani
Anand
*/
console.log(char);
}/* output
A
b
d
u
l
K
a
l
a
m
*/
let chair = {
material: "plastic",
price: 5000
}
console.log(prop);
console.log(index);
} /*output
0
1
2
3
4
5
*/
console.log(studentList[index]);
} /* output
Bhagat singh
Vivekanand
Abdul Kalam
Vinobha bhave
let chair = {
material: "plastic",
price: 5000
}
console.log(key);
}/ *output:-
company
material
type
price
*/
console.log(chair[key]);
} /*output
Neel Kamal
plastic
office chair
5000
*/
Date
Whenever working with Dates, use the new Date() object.
1.new Date()
3.new Date(milliseconds)
1. new Date() this object creates a new date object with the current time
and date
Note:- By default, JavaScript use the browser's time zone and display a
date as a full text string
this object creates new date object with specified time and date this
object accepts 7 parameters in a same order
let specifiedDate = new Date(2010, 11, 30, 05, 33, 45, 300);
fullYear = pass four-digit number to set year. If you parsed two digits or
only one digit, JavaScript interprets it by prefixing 19.
let date1 = new Date(3, 01, 13, 10, 25, 13, 100);
let date2 = new Date(10, 01, 13, 10, 25, 13, 100);
0 = January, 11 = December
let date4 = new Date(2020, 11, 20, 05, 12, 02, 310);
specifying month higher than range will not give any error just it will
overflow to next year
specifying a date higher than 31 will not give an error, overflow to the
next month
let date6 = new Date(2010, 02, 33, 12, 02, 18, 620);
specifying an hour higher than 23 will not give an error, overflow to the
next day
specifying minutes higher than 59 will not give an error, overflow to the
next hour
specifying seconds higher than 59 will not give an error, overflow to the
next minutes
let date7 = new Date(2010, 10, 01, 24, 60, 70, 00);
We can skip parameters until the date the skip parameter will be stored
as default values
3. new Date(milliseconds)
this object creates a new date object with milliseconds. This object
accepts only one parameter is milliseconds
3.Long date -> new Date("mm dd yyyy") or new Date("dd mm yyyy ")
let birthDate = new Date(2010, 02, 25, 03, 25, 56, 300);
console.log(birthDate.getMonth()); // output:- 2
console.log(birthDate.getDate()); // output 25
console.log(birthDate.getHours()); // output 3
console.log(birthDate.getMinutes()); // output 25
console.log(birthDate.getSeconds()); // output 56
myDate1.setFullYear(1991);
myDate1.setMonth(05);
myDate1.setDate(17);
myDate1.setHours(07);
myDate1.setMinutes(55);
myDate1.setSeconds(01);