Simple Js hacks for strings and objects 🙌
Himanshupal0001
Posted on July 16, 2022
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' ]
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 }
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' }
💖 💪 🙅 🚩
Himanshupal0001
Posted on July 16, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.