Simple Js hacks for strings and objects 🙌
Himanshupal0001

Himanshupal0001 @himanshupal0001

About: Full stack web Dev. Like to teach and learn. Open for free lance and open source projects.

Location:
DXB
Joined:
Nov 21, 2021

Simple Js hacks for strings and objects 🙌

Publish Date: Jul 16 '22
5 0

Tricks

This document contain the simple yet important tricks to remember when dealing with strings or objects.

1. Convert any string to array simply by a single function.

Use split("", limit{any number})

//code

const str = 'abra ka dabra'
console.log(str.split(' ', 2))

output 
[ 'abra', 'ka' ]
Enter fullscreen mode Exit fullscreen mode

2. Add properties to js objects dynamically

Use (.) operator

//code

const dynamicObject = {
    name: 'Himanshu',
    age: 22
};
dynamicObject.sex = 'Male';

console.log(dynamicObject);

//output

{ name: 'Himanshu', age: 22, sex: 12 }
Enter fullscreen mode Exit fullscreen mode

3. Adding properties of a previous object to new object


//code
const object1 = {
    name : 'Himanshu',
    age : 22
}

const newObject  = {};
newObject.name = object1.name;
newObject.age = object1.age;
newObject.sex = 'Male';

console.log(newObject);


//output
{ name: 'Himanshu', age: 22, sex: 'Male' }
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment