How to check the existence of a key in JavaScript Object const myObj = { a: 1, b: 2}; We need to check whether I’d exist in object or not! const result = myObj.hasOwnProperty('id'); // false Or const result = 'id' in myObj; // false Or const result = myObj.id !== undefined; // false
How to check if value id null or undefined. simple way is just check if has value! Like if(value) { // ... } will return true if value is not: null, undefined, NaN, empty string (""), 0, false It’s fulfill our requirements but also check additional conditions Like we want to check id is null or undefined but in response we got id = 0 In this condition it will fail because we just need to check null or undefined if(value !== undefined || value !== null){ // ... }
We can use spread operator to add two array const result = [...arr1, ...arr2]; We can copy/clone one array/object to other variable const arr3 = [...arr1]; const obj1 = {...obj1}; Extend array value const arr4 = [ 1, 3 , ...arr1]; function myFunction(...theArgs) { // } in react we can pass all props using sprad property &lgt
Comments