DEV Community

Aamir Khan
Aamir Khan

Posted on

Javascript Basics

Spread Operator

const boo = [1, 2]
const foo = [3, 4]
const zoo = [...boo, ...foo]
console.log(zoo)
// prints [ 1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

Arrow Function

const arrow = (a, b) => a + b
console.log(arrow(2, 10))
// prints 12
Enter fullscreen mode Exit fullscreen mode

Function with Default Parameters

function fundef(a = 10) {
    console.log(a)
}
fundef() // prints 10
fundef(29) // prints 29
Enter fullscreen mode Exit fullscreen mode

Template literals (Template strings)

const word1 = 'Hello'
const word2 = `${word1} World`
console.log(word2)
// prints "Hello World"
Enter fullscreen mode Exit fullscreen mode

String includes()

console.log('orange'.includes('ge')); // prints true
console.log('orange'.includes('zz')); // prints false
Enter fullscreen mode Exit fullscreen mode

String repeat()

console.log('ab'.repeat(4))
// prints "abababab"
Enter fullscreen mode Exit fullscreen mode

String startsWith()

console.log('orange'.includes('or')); // prints true;
console.log('orange'.includes('ge')); // prints false;
Enter fullscreen mode Exit fullscreen mode

Destructing Array

let [a, b] = [1, 2]
console.log(a); // prints 1
console.log(b); // prints 2
Enter fullscreen mode Exit fullscreen mode

Destructing Object

let obj = {
    a1: 10,
    b2: 20
};
let { a1, b2 } = obj;
console.log(a1); // prints 10
console.log(b2); // prints 20
Enter fullscreen mode Exit fullscreen mode

Object.assign()

const obj1 = { q: 12 }
const obj2 = { a: 33 }
const obj3 = Object.assign({}, obj1, obj2)
console.log(obj3); // prints { q:12, a: 33 }
Enter fullscreen mode Exit fullscreen mode

Destructuring Nested Objects

const item = {
    device: "iPadPro",
    brand: "apple",
    year: 2020,
    accessories: {
        pencil: "2ndGen",
        charger: "30W",
    }
}
const { accessories: { pencil, charger }, device } = item
console.log(device, pencil, charger); 
// prints "iPadPro 2ndGen 30W"
Enter fullscreen mode Exit fullscreen mode

Spread Operator

const v1 = {
    firstName: "Web",
    lastName: "Developer"
}
const v2 = {
    ...v1,
    lastName: "Tips",
    sex: "NA"
}
console.log(v1); 
// prints { firstName: 'Web', lastName: 'Developer' }

console.log(v2); 
// prints { firstName: 'Web', lastName: 'Tips', sex: 'NA' }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)