TypeScript:
Watch mode
Watch mode will always compile the code with automatic saving the file in VS code.
tsc --init // when we hit, it tells the typescript to be create a config.json file and automate to save the code and show compilation errors.
Let Vs const
const variable values cannot be changed.
let is the key word where the variable will be available in that block.
Arrow function
//Arrow function
const adding = (a:number, b:number) => {
return a + b;
};
// the below example doesn't require return, which means direct implement notation doesn't require return
const substraction = (a:number, b:number) => (b - a);
console.log(adding(2,5));
Spread Operations
//Spread Operators:
const hobbies = ["walking"];
const activehobbies = ["eating","sleeping"];
activehobbies.push(hobbies[0]);
console.log(activehobbies);
//now the spread operator is alternative way to push
const spreadarrayhobbies = ["tv","music","news",...activehobbies];
console.log(spreadarrayhobbies);
//other example with object about copy
const cupii = {
"name":"dummy name",
"age":185
}
const copyarray = {...cupii};
console.log(copyarray.name);
Rest Parameter
Rest parameters allow n-number of parameters by passing the parameter in the form of array
Note: Instead of using for loop in array, we can also use reduce
//Rest Parameters
const addVa = (...numbers:number[]) => {
return numbers.reduce((curResult, curValue) => {
return curResult + curValue
},0);
return numbers
}
const myRestParameterArr = addVa(1, 2, 3, 4, 5);
console.log(myRestParameterArr);
Comments
Post a Comment