Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //loop (way of looping)
- //function (default parameter)
- //Destructing, spreading
- //nested object, array access
- //Loop(Repeating things)
- // for (let i = 0; i < 10; i++) {
- // let occurrence
- // if (i <= 1) {
- // occurrence = 'time'
- // } else {
- // occurrence = 'times'
- // }
- // console.log(`I Love Bangladesh ${i} ${occurrence}`)
- // }
- //Looping array and get the sum
- // function sum(arr) {
- // let sum = 0
- // for (let i = 0; i < arr.length; i++) {
- // sum = sum + arr[i]
- // }
- // console.log(sum)
- // }
- // sum([1, 2, 3, 4])
- //Extra (passing regular value and looping up t the point)
- // function sum(num) {
- // let sum = 0
- // for (let i = 0; i < num; i++) {
- // console.log(i)
- // sum += i
- // // sum = sum + arr[i]
- // }
- // console.log(sum)
- // }
- // sum(4)
- //Looping array
- // const arr = [1, 2, 3, 4]
- // for(let i = 0; i < arr.length; i++){
- // console.log(i)
- // console.log(arr[i])
- // }
- //automatic semicolon insertion(Interview Question) and Odd behavior
- //default parameter
- // function showInfo(name = 'samim', random, country = 'Bangladesh') {
- // return `I am ${name} and I Love ${country}`
- // }
- // console.log(showInfo('Hasan', 'India', 'Pakistan'))
- //More common and generalized loop
- //simple for loop
- //while loop, do while loop
- //array specific
- //forEach
- //for of
- //for in (NotRecommended- No order)
- //array helper method(map, reduce, ...)
- //object specific
- //for in
- //forEach loop
- // const arr = [1, 2, 3, 4]
- // arr.forEach(num => {
- // console.log(num)
- // })
- //for of loop
- // for(let num of arr){
- // console.log(num)
- // }
- // const obj = {
- // firstName: 'samim',
- // lastName: 'Hasan'
- // }
- //Looping object(for in)
- // for(let prop in obj ){
- // console.log(obj[prop])
- // // console.log(prop)
- // }
- //Applying array looping technique with object using keys() values() entries()
- // console.log(Object.entries(obj))
- // for (let prop of Object.entries(obj)) {
- // console.log(prop[1])
- // }
- // for (let prop in obj.keys()) {
- // console.log(obj[prop])
- // // console.log(prop)
- // }
- // const arr = [1, 2, 3, 4]
- // const num1 = arr[0]
- // const num2 = arr[1]
- // const num3 = arr[2]
- // const num4 = arr[3]
- const arr = [1, 2, 3, 4]
- //destructuring
- // const[num1, , num3, num4] = arr
- //rest operator
- const [num1, ...restArr] = arr
- console.log(num1, restArr)
- const obj = {
- firstName: 'samim',
- lastName: 'Hasan',
- age: 27
- }
- //Object destructuring and rest operator
- const {lastName:lName = 'blabla' , ...restObj} = obj
- console.log(restObj)
- // console.log(fName, lName)
- // const arr = [1, 2, 3, 4]
- //spreading arry
- // const newArr = [...arr, 6]
- // console.log(newArr)
- //spreading object
- const copiedObj = {
- ...obj,
- profession: 'web developer'
- }
- console.log(copiedObj)
- //remember(!!) Extra [] in using rest values as function parameter
- // Little useful technique
- // function showProfile({firstName, ... rest}){
- // console.log(firstName, rest)
- // }
- // showProfile({
- // firstName: 'samim',
- // lastName: 'Hasan',
- // age: 27
- // })
- //nested array and object value accessing
- const obj1 = {
- obj2: {
- obj3: {
- hi: 'Hello',
- nums: [1, 2, 3]
- }
- }
- }
- console.log(obj1.obj2.obj3.nums[2])
- const arr1 = [[1, 3, [10, 12], 4], 6, [9, 10]]
- console.log(arr1[0][2][1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement