CODE's

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

1. let, var and const.

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*/

/* 1. let and const is locally scoped element, whereas var is gobally


scoped element
2. let and const didn't allow to re-declare element with same name,
whereas var allows to re-declare element with same name
3. We cannot change the value of const after declaration, but we can
change the value of let after declaration*/

// var b=50 var allowed to re-declare element with same name


//let a=10 let didn't allowed to re-declare element with same name
// c=10 cannont assign the value of const variable

console.log(a)// console.log() is used to print something

{
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'

2."content" -->using double quote


let b="Anas"

3.`content` -->using backtic


let c=`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 allows to change the content of string if we update


the string */

//Template litrals

// let str_1="Anas"
// let data=`${str_1} is a boy`
// console.log(data)

/*Some strings methods in java script

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`.

4. concat(str1, str2, ...):


- Combines two or more strings and returns a new string.
- Example: `"Hello".concat(" ", "World")` returns `"Hello World"`.

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`.

9. localeCompare(compareString, locales, options):


- Compares two strings in the current locale and returns -1, 0, or 1
depending on whether the string comes before, is equal to, or comes
after the compareString.
- Example: `"a".localeCompare("b")` returns `-1`.

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é"`.

13. padEnd(targetLength, padString):


- Pads the current string with another string (repeated, if needed)
so that the resulting string reaches the given length.
- Example: `"Hello".padEnd(8, " world")` returns `"Hello wo"`.

14. padStart(targetLength, padString):


- Pads the current string with another string (repeated, if needed)
so that the resulting string reaches the given length.
- Example: `"Hello".padStart(8, " world")` returns `" woHello"`.

15. repeat(count):
- Returns a new string which contains the original string repeated
`count` times.
- Example: `"Hello".repeat(3)` returns `"HelloHelloHello"`.

16. replace(searchValue, replaceValue):


- Searches a string for a specified value, or a regular expression,
and returns a new string where the specified values are replaced.
- Example: `"Hello".replace("l", "w")` returns `"Hewwo"`.

17. replaceAll(searchValue, replaceValue):


- Returns a new string with all matches of a pattern replaced by a
replacement.
- Example: `"Hello Hello".replaceAll("l", "w")` returns `"Hewwo"`.

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`.

19. slice(start, end):


- Extracts a section of a string and returns a new string.
- Example: `"Hello".slice(1, 3)` returns `"el"`.

20. split(separator, limit):


- Splits a string into an array of substrings based on a specified
separator.
- Example: `"Hello World".split(" ")` returns `["Hello", "World"]`.

21. startsWith(searchString, position):


- Checks if a string starts with the specified characters.
- Example: `"Hello".startsWith("He")` returns `true`.

22. substring(indexStart, indexEnd):


- Returns the substring between `indexStart` and `indexEnd` (or end
of string) as a new string.
- Example: `"Hello".substring(1, 4)` returns `"ell"`.

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"`.

29. trimEnd() (deprecated in favor of trimRight()):


- Removes whitespace from the end of a string.
- Example: `" Hello ".trimEnd()` returns `" Hello"`.

30. trimStart() (deprecated in favor of trimLeft()):


- Removes whitespace from the beginning of a string.
- Example: `" Hello ".trimStart()` 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
*/

// To declare array in java


let arr=[10,"Anas",undefined,null,BigInt(19),90,70,true]// Here we can
insert different data type element in same array
console.log(arr,typeof(arr))
let arr_num=[10,20,30]
console.log(arr_num,typeof(arr_num))
//The default typeof array is object

arr_num[10]=1000//We can add element in array with using index


console.log(arr_num)

arr_num[1]="Changed Value"//We can change the value of any element in


array with using the index
console.log(arr_num)

//Some array function


//1.toString()

let arr_string =arr_num.toString()


console.log(arr_string)

//makes new array to string and it is seperated by ,

//2.join()
{
let arr=[10,20,30,40]
let arr_join=arr.join(" = ")
console.log(arr_join)

//makes new array to be seperated by parameter passed in join function


}

//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)

//delete is used to delete the element at the specified index and


the important thing about delete is that the memory of deleted element
is still present in an array

//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
}

//To access the element with forEach


{
let arr=[10,20,30,40,50]
arr.forEach(element => {
console.log(element+element)
});

//contains three argument (element/value,index,whole array)


}

//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)
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy