Java Script Basic
Spread Operator
the operator shap is three consective dots and is written as:
Usages of sparead operator
1. Expanding String - Convert String into list of array
2. Combining Arrays - Combine array or add value to array
3. Combining Object - Combine Object or add value to Object
4. Create New Shallow Copy of Arrays and Object
sparead operator
1) Arrays
var arry = [a,b,c];
// array indax start from zero
console.log(arry[0]);
console.log(arry[1]);
//2 Object
var obj = {
"name" : "salesforce",
"age" : "54",
}
console.log(obj.name)
console.log(obj.age)
// Spread Operator
//1. Expanding of string
let greeting = "Hello World"
let charlist = [...greeting]
console.log(charlist[10]);
//2.Combing array
let arr1 = ["amazon","persistent","google"];
let arr2 = ["feedback","instration"]
let arr3 = [...arr1, ...arr2];
console.log(arr3);
// 3.adding values to an array
let arr4 = ["a","b","c"];
let arr5 = [...arr4,"giri"];
console.log(arr5);
// 4. Combining Object
let obj1 = {name:"salesforce", age:23, date:"23/10/1990}
let obj2 = {name:"feedback", age:26}
let obj3 = {...obj1, ...obj2}
//5. Shallow copy
var arr10 = ["x","y","z"]
arr10.push(20)
console.log(arr10)
console.log(arr11)
//6. Nasted copy
var arrObj1 = [{name:"giri"},
{name:"salesforce"}]
var arrobj1 = [...arrobj]
Destructring
The two most used data structure in javaScript are Object and Arrays
Destructring is a special syntax that allows us to "unpack" arrays or objects into a
bunch of variables as sometimes that's more convenient
. Arrays destructing
. Object destructing
// array destructing
let arr = ["amazon", "google"]
let [company1, company2] = arr
console.log(company1)
console.log(company2)
// object destructing
let options = {
title:"zero to hero",
age :23,
type : "CRM"}
let {title, age, type} = options
console.log(title)
console.log(age)
console.log(type)
String interpolation
. string interpolation allows you to embed expressions in the string
. Template string use back-tick('') rather than the singe or double-quotes
var name = "salesforce troop";
console.log('Hello, ${name}!');
//string Interpolation with expressions
var a= 10;
var b = 20;
console.log('The sum of ${a} and ${b} is ${a+b}');
//string Interpolation
var name = "sales"
var name1 = "force"
console.log(name + name1);
var name = "Giri Kumar"
var age = 35
var str = "my name is "+ name+"and my age is "+ age
console.log(str)
var a = 2
var b = 3
var str1 = "the sum of"+a+ "and"+b+ "is" a+b
console.log(str1)
String Method
JavaScript provides many methods to play with strings. Below are some of the most commonly used string methods
in LWC
1.includes()
2.indexOf()
3.StartsWith()
4.Slice()
5.toLowerCase()
6.toUpperCase()
7.trim()
let str = "Hello guys i hope your are doing good"
//Include
//The includes() method determines whether a string contains
//the characters of specialfied string
let check = str.includes("hope")
console.log(check)
// indexof
// The indexof() method returns the posities of the first
// occurrence of a specified value in a string
let index = str.indexof("doing")
console.log(index)
//stratWith()
// determines whether a string begins with the characters
// of a specified string
console.log(str.startWith("Hello"))
//slice()
//method extracts parts of a string and returns the
//extracted parts in a new string
let newstr = str.slice(0, 5)
console.log(newstr)
//toLowerCase
// converts string to lowercases letter
let x = "my name is giri "
console.log(x.toLowerCase())
//trim() - remove whitespace from both sides of a string
let searchtext = " salesforce lwc "
console.log(searchtext.trim())
let obj = {
name: "Salesforce",
age : 23,
dob : '20/03/2024'
}
console.log(Object.keys(obj))
// Object.keys()
console.log(object.keys(obj))
// Object.values()
console.log(object.values(obj))
//JSON.stringify
let str = JSON.stringify(obj))
console.log(str)
//JSON.parse
console.log(JSON.parse(str))
Arrays Method
1) map() - loop over the array and return new array based on the value return.
2) every() - return ture or false if every element in the array satisfy the condition.
3) filter() - return new array with all the element that satisfy the condition
4) some() - return true if at least one element in the array satisfy
5) sort() - sort the element of an array
6) reduce() - this method reduces the array to asigle value(left to right)
7) foreach() - this method calls for each array element
let arr = [2,3,4,5,6,7,9,10]
let newArray = arr.map(funcation(currentItem, index, array){
console.log('currentItem is ${currentItem},index ${index},array ${array}' )
return currentItem*2
})
console.log(newArray)
//filter()
let filteredvalues = arr.filter(funcation(currentItem, index, array){
return currentItem > 5
})
console.log(filteredvalues)
//every
let age = [32, 33, 18, 40]
let agealladult = age.every(funcation(currentItem){
return currentItem >=18
})
//some
let agelist = [32, 33, 18, 40]
let isadult = agelist.some(funcation(currentItem){
return currentItem > 32
})
console.log(isadult)
//sort()
var names = ["giri","hari","john"]
console.log(names.sort())
//sorting of number
var points = [10,32,12,80,54]
points.sort(funcation(a,b){
return a-b
})
console.log(sortedValue)
let num = [12, 78, 30]
let sum = num.reduce(funcation(total,currentItem){
return total+currentItem
},0)
console.log(sum)
//for Each
num.forEach(funcation(currentItem){
console.log(currentItem)
})
Promise
Promise is an object that may produce a single value sometime in the future
promise are used to handle asynchronous operations in javaScript
A Promise has three states
1) pending
2) fulfilled
3) rejected
Use Case from LWC point of view
1. Fetching data from server 2) Loading file from system
let index = str.indexOf("doing")
console.log(index)
Comments
Post a Comment