CODE's
CODE's
CODE's
let a=10
var b=30
const c=10
/*we can end statement with semi-colon..but if we didn't end with semi-
colon then also code executes*/
{
let a=90 // here we can we re-declare element with same name
}
2. Primitive and object.
//Primaitive data type
//nn ss bb u
/*
n-number
n-null
b-bigInt
b-boolean
s-string
s-symbol
u-undefined
*/
let a=10//number
let b=null//null
let c=BigInt(5637)//bigint
let d=true//boolean
let e="Anas"//string
let f=Symbol("This is the example of symbol")//symbol
let g=undefined//undefiend
//object
const obj={
Name:"Anas",
Age:18,
Male:true
}
console.log(obj["Name"])
3. Operation and expression.
/*Assignment operators
1.+
2.-
3.*
4./
5.** -->exponentiation operator(45**4 = 45^4)
6.%
7.++
8.--
*/
let a=10, b=10
console.log(a+b)
console.log(a-b)
console.log(a*b)
console.log(a/b)
console.log(a**b)
console.log(a%b)
console.log(++a)
console.log(--b)
/*Assignment Operator
1.=
2.+=
3.-=
4.*=
5./=
6.%=
7.**=
*/
let c=40,d=23
console.log(c=d)//now c=23
console.log(c+=d)
console.log(c-=d)
/*Comparision operators
1.==
2.!=
3.=== -->both values are equal and the type is also same
4.!== -->both values are not equal and the type is also not same
5.>
6.<
7.>=
8.<=
9.?
*/
let e=10,f=20,g="10"
console.log(e==f)
console.log(e==g)
console.log(e!=f)
console.log(e===g)
console.log(f!==g)
console.log(e>f)
console.log(e<f)
console.log(e>=f)
console.log(e<=f)
/*Logical Operator
1.&&
2.||
3.!
*/
4. Loops
/*Types of Loops in java script
1.for loop
2.for in loop -->loops through key of an object
3.for of loop -->loops through the value of an object
4.while loop
5.do while loop
*/
5. Functions
/*Two ways are to define functions in java script
1.
function function_name(parameter_1,parameter_2,....parameter_n){
body of function
}
2.
const function_name=(parameter_1,parameter_2,....parameter_n)=>{
body of function
}
*/
function sum(a,b){
return a+b
}
const sub=(a,b)=>{
return a-b
}
let a=10,b=20
console.log(sum(a,b))//sum function
console.log(sub(a,b))//sub function
6.Strings
/*We can declare strings in 3 ways in java script
1.'content' -->using single quote
let a='Anas'
/*While using '' quote we cant use ' in our string and same as using ""
quote we cant use " is our string but while using backtic(``) we can
use "" quote and '' quote in our string. And using `` we can create
template litrals*/
//Template litrals
// let str_1="Anas"
// let data=`${str_1} is a boy`
// console.log(data)
1. charAt(index):
- Returns the character at the specified index in the string.
- Example: `"Hello".charAt(1)` returns `"e"`.
2. charCodeAt(index):
- Returns the Unicode value of the character at the specified index.
- Example: `"Hello".charCodeAt(0)` returns `72` (Unicode for "H").
3. codePointAt(index):
- Returns the Unicode code point value of the character at the
specified index.
- Example: `"😊".codePointAt(0)` returns `128522`.
5. endsWith(searchString, endPosition):
- Checks if a string ends with the specified characters.
- Example: `"Hello".endsWith("lo")` returns `true`.
6. includes(searchString, position):
- Checks if a string contains the specified characters.
- Example: `"Hello".includes("ell")` returns `true`.
7. indexOf(searchValue, fromIndex):
- Returns the index within the calling string object of the first
occurrence of the specified value, starting the search at `fromIndex`.
- Example: `"Hello".indexOf("l")` returns `2`.
8. lastIndexOf(searchValue, fromIndex):
- Returns the index within the calling string object of the last
occurrence of the specified value, starting the search at `fromIndex`.
- Example: `"Hello".lastIndexOf("l")` returns `3`.
10. match(regexp):
- Retrieves the result of matching a string against a regular
expression.
- Example: `"Hello".match(/[a-z]+/)` returns `["Hello"]`.
11. matchAll(regexp):
- Returns an iterator of all results matching a string against a
regular expression.
- Example: `"Hello".matchAll(/[a-z]+/)` returns an iterator object.
12. normalize(form):
- Returns the Unicode Normalization Form of the string.
- Example: `"résumé".normalize()` returns `"résumé"`.
15. repeat(count):
- Returns a new string which contains the original string repeated
`count` times.
- Example: `"Hello".repeat(3)` returns `"HelloHelloHello"`.
18. search(regexp):
- Searches a string for a specified value or regular expression and
returns the position of the match.
- Example: `"Hello".search("l")` returns `2`.
23. toLocaleLowerCase():
- Returns the string converted to lowercase according to any
locale-specific case mappings.
- Example: `"Hello".toLocaleLowerCase()` returns `"hello"`.
24. toLocaleUpperCase():
- Returns the string converted to uppercase according to any
locale-specific case mappings.
- Example: `"Hello".toLocaleUpperCase()` returns `"HELLO"`.
25. toLowerCase():
- Returns the string converted to lowercase letters.
- Example: `"Hello".toLowerCase()` returns `"hello"`.
26. toString():
- Returns the value of a String object.
- Example: `String("Hello").toString()` returns `"Hello"`.
27. toUpperCase():
- Returns the string converted to uppercase letters.
- Example: `"Hello".toUpperCase()` returns `"HELLO"`.
28. trim():
- Removes whitespace from both ends of a string.
- Example: `" Hello ".trim()` returns `"Hello"`.
31. valueOf():
- Returns the primitive value of a String object.
- Example: `String("Hello").valueOf()` returns `"Hello"`.
*/
//1. charAt:
console.log("Anas".charAt(3))
//2.concat():
console.log("My name is ".concat("Anas"))
//3.endsWith:
console.log("Anas".endsWith("as"))
//4.includes:
console.log("Anas Qureshi".includes("nas"))
//5.indexOf:
console.log("Hello World".indexOf("e",5))
//6.lastIndexOf:
console.log("Hello".lastIndexOf("a"))
7. Arrays
/*Array is used to store multiple values in under same name
-Array in java can hold different data type element under same name
-When we delete some element is array in java using delete method
then the element is only deleted the memory doesn't dispatch
-Arrays are mutable
*/
//2.join()
{
let arr=[10,20,30,40]
let arr_join=arr.join(" = ")
console.log(arr_join)
//3.pop()
{
let arr=[10,20,30,40,50]
arr.pop()
console.log(arr)
//pops the last inserted element in array and pop returns the poped
element
}
//4.push()
{
let arr=[10,20,30,40]
arr.push("Pushed Element")
console.log(arr)
//push is used to insert the element passed in push method at the
end of array
}
//5.shift()
{
let arr=[10,20,30,40]
arr.shift()
console.log(arr)
//it is used to delete element in existing array at 0th index
}
//6.unshift()
{
let arr=[20,30,40]
arr.unshift(10)
console.log(arr)
//it is used to add the new element at the 0th index of existing
array
}
//7.delete
{
let arr=[10,20,30,40]
delete arr[2]
console.log(arr)
//8.concat()
{
let arr_1=[10,20,30]
let arr_2=["Array_2_Item_1","Array_2_Item_2"]
let arr_3=["Array_3_Item_1","Array_3_Item_2"]
console.log(arr_1.concat(arr_2,arr_3))
//it is used to concatinate the n number of array
}
//9.sort()
{
let arr=[2,4,1,8,9,10,90]
arr.sort()
console.log(arr)
//sort method without parameter sort the element in array in
alphabetical order
//To sort the number in assending and descending order we have to
pass a compare method
const compare=(a,b)=>{
return a-b// return a-b is used to sort the element in
asscending order
}
arr.sort(compare)
console.log(arr)
const compare1=(a,b)=>{
return b-a// return b-a is used to sort the element in
descending order
}
arr.sort(compare1)
console.log(arr)
}
//10.reverse()
{
let arr=[10,20,30,40,50]
arr.reverse()
console.log(arr)
// reverse() method is used to reverse the array
}
//11.splice()
{
let arr=[10,20,30,40,50,60]
arr.splice(2,0,10000,10001,10002)
console.log(arr)
//splice method is used to go to the specified index and delete the
number of element specified from index and then insert the all element
which are given in splice method
}
//12.slice()
{
let arr=[10,20,30,40,50,60]
let arr_slice=arr.slice(2,5)
console.log(arr_slice)
//It takes one or two argument if only one argument is passed then
it create the new array from that index till the end, and if 2 argument
is passed then it creates the new array from starting index till the
value of last index - 1
}
//Array.from
//It is used to create the array from object
{
let name="Anas"
let name_arr=Array.from(name)
console.log(name_arr)
//Array.from is used to create the array of any object
}
//map
//It used to make new array from existing array
{
let arr=[10,20,30,40,50]
let arr_copy=arr.map(element=>{
return element
})
console.log(arr_copy)
//contains three argument (element/value,index,whole array)
}
//filter
//it is the method which is used to create new array based on condition
{
let arr=[10,20,39,40,59]
let arr_filtered=arr.filter(element=>{
return (element%2==0)
})
console.log(arr_filtered)
}
//reduce
//It is used to do the operation on which is passed in function
{
let arr=[10,20,30,40,50,60]
let arr_reduce=arr.reduce((element1,element2)=>{
return element1+element2
})
console.log(arr_reduce)
}