---
title: "Purvi Barot (@purvi) | Hashnode"
description: "Software Engineer. Read the latest articles by Purvi Barot on Hashnode."
url: "https://hashnode.com/@purvi"
---

@purvi

# Purvi Barot

@purvi · Joined July 2019

Software Engineer

[Website](https://www.ifourtechnolab.com/custom-ecommerce-development-company)

## Comments

[To sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects. Suppose that you have an array of employee objects as follows: let employees = \[ { firstName: 'John', lastName: 'Doe', age: 27, joinedDate: 'December 15, 2017' }, { firstName: 'Ana', lastName: 'Rosy', age: 25, joinedDate: 'January 15, 2019' }, { firstName: 'Zion', lastName: 'Albert', age: 30, joinedDate: 'February 15, 2011' } \]; The following statement snippet sorts the employees array by ages in ascending order: employees.sort((a, b) => { return a.age - b.age; }); To display the employees, you use the forEach() method: employees.forEach((e) => { console.log(\`${e.firstName} ${e.lastName} ${e.age}\`); }); To sort the employees by ages in descending order, you just need to reverse the order in the comparison function. For example: employees.sort((a, b) => b.age - a.age); employees.forEach((e) => { console.log(\`${e.firstName} ${e.lastName} ${e.age}\`); });](https://hashnode.com/posts/sort-array-of-object-in-javascript/603e6c6fff443a0a4499df6a/comment/603f1339fb37920a1fc642ef)

Comment · Article · Mar 3, 2021 · [Sort array of object in javascript](https://hashnode.com/posts/sort-array-of-object-in-javascript/603e6c6fff443a0a4499df6a)

[A function is a group of statements that together perform a specific task. You can write all intended functionality at one place and call it wherever required. Why use function ? Function are used for divide a large code into module, due to this we can easily debug and maintain the code. For example if we write a calculator programs at that time we can write every logic in a separate function (For addition sum(), for subtraction sub()). Any function can be called many times. Defining a function: return\_type function\_name(parameter) { function body; } Calling a function: function\_name(); OR variable=function\_name(argument); Advantage of Function: Code Re-usability Develop an application in module format. Easily to debug the program. Code optimization: No need to write lot of code. Length of the source code can be reduced.](https://hashnode.com/posts/why-use-functions/603136f372a689446db3df14/comment/603734bc05efef7f0a9d4507)

Comment · Article · Feb 25, 2021 · [Why use functions?](https://hashnode.com/posts/why-use-functions/603136f372a689446db3df14)

[Here is an example that shows the differences: var s1 = new String("Avoid newing things where possible"); var s2 = "A string, in TypeScript of type 'string'"; var s3: string; String is the JavaScript String type, which you could use to create new strings. Nobody does this as in JavaScript the literals are considered better, so s2 in the example above creates a new string without the use of the new keyword and without explicitly using the String object. string is the TypeScript string type, which you can use to type variables, parameters and return values.](https://hashnode.com/posts/the-difference-between-string-and-string-in-typescript/6022db0c3f7a783c6be412a3/comment/6026063ac6d91052dafe93ec)

Comment · Article · Feb 12, 2021 · [The difference between String and string in Typescript](https://hashnode.com/posts/the-difference-between-string-and-string-in-typescript/6022db0c3f7a783c6be412a3)

[!! converts the value to the right of it to its equivalent boolean value. (Think poor man's way of "type-casting"). Its intent is usually to convey to the reader that the code does not care what value is in the variable, but what it's "truth" value is. So !! is not an operator, it's just the ! operator twice. Real World Example "Test IE version": const isIE8 = !! navigator.userAgent.match(/MSIE 8.0/); console.log(isIE8); // returns true or false If you: console.log(navigator.userAgent.match(/MSIE 8.0/)); // returns either an Array or null But if you: console.log(!!navigator.userAgent.match(/MSIE 8.0/)); // returns either true or false](https://hashnode.com/posts/what-is-the-not-not-operator-in-javascript/60200cf4636acc2a3822fd93/comment/6020bb790da0382a2d49e57a)

Comment · Article · Feb 8, 2021 · [What is the !! (not not) operator in JavaScript?](https://hashnode.com/posts/what-is-the-not-not-operator-in-javascript/60200cf4636acc2a3822fd93)

[TypeScript is a superset of JavaScript which primarily provides optional static typing, classes, and interfaces. One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as you type the code. Example: As an example, here's some TypeScript. class Greeter { greeting: string; constructor (message: string) { this.greeting = message; } greet() { return "Hello, " + this.greeting; } } And here's the JavaScript it would produce var Greeter = (function () { function Greeter(message) { this.greeting = message; } Greeter.prototype.greet = function () { return "Hello, " + this.greeting; }; return Greeter; })(); Notice how the TypeScript defines the type of member variables and class method parameters. This is removed when translating to JavaScript but used by the IDE and compiler to spot errors, like passing a numeric type to the constructor.](https://hashnode.com/posts/why-typescript-is-the-better-choice-than-javascript/60169494e54673573b572ba7/comment/601799ef6f46cc1ff950ad72)

Comment · Article · Feb 1, 2021 · [Why Typescript is the better choice than JavaScript?](https://hashnode.com/posts/why-typescript-is-the-better-choice-than-javascript/60169494e54673573b572ba7)

[A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method's parameters. Given the following example function that adds two integers, x and y would be referred to as its parameters: int add(int x, int y) { return x + y; } At a call-site using add, such as the example shown below, 123 and 456 would be referred to as the arguments of the call. int result = add(123, 456); Also, some language specifications (or formal documentation) choose to use parameter or argument exclusively and use adjectives like formal and actual instead to disambiguate between the two cases. For example, C/C++ documentation often refers to function parameters as formal arguments and function call arguments as actual arguments.](https://hashnode.com/posts/know-the-difference-between-argument-and-parameter-in-programming/600ff9459d3cb25c41e0b347/comment/6010e3f6b5b03f4bf00e46bb)

Comment · Article · Jan 27, 2021 · [Know the difference between Argument and Parameter in programming](https://hashnode.com/posts/know-the-difference-between-argument-and-parameter-in-programming/600ff9459d3cb25c41e0b347)

[The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would. const object1 = { a: 'somestring', b: 42, c: false }; console.log(Object.keys(object1)); // expected output: Array \["a", "b", "c"\] let unorderedData = { real\_name: 'Millie Bobby Brown', character\_name: 'Eleven', series: 'Stranger Things' }; console.log(JSON.stringify(unorderedData)); const orderedData = {}; Object.keys(unorderedData).sort().forEach(function(key) { orderedData\[key\] = unorderedData\[key\]; }); console.log(JSON.stringify(orderedData));](https://hashnode.com/posts/prevent-an-object-key-from-appearing-in-objectkeys-or-forin-loop/5ff32d8d72eafd0f1c8c66af/comment/600656f2848f4c3b9a0f21a0)

Comment · Article · Jan 19, 2021 · [Prevent an object key from appearing in \`Object.keys()\` or \`for..in\` loop](https://hashnode.com/posts/prevent-an-object-key-from-appearing-in-objectkeys-or-forin-loop/5ff32d8d72eafd0f1c8c66af)

[The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified. console.log(JSON.stringify({ x: 5, y: 6 })); // expected output: "{"x":5,"y":6}" console.log(JSON.stringify(\[new Number(3), new String('false'), new Boolean(false)\])); // expected output: "\[3,"false",false\]" console.log(JSON.stringify({ x: \[10, undefined, function(){}, Symbol('')\] })); // expected output: "{"x":\[10,null,null,null\]}" console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5))); // expected output: ""2006-01-02T15:04:05.000Z"" Examples using JSON.stringify: JSON.stringify({}); // '{}' JSON.stringify(true); // 'true' JSON.stringify('foo'); // '"foo"' JSON.stringify(\[1, 'false', false\]); // '\[1,"false",false\]' JSON.stringify(\[NaN, null, Infinity\]); // '\[null,null,null\]' JSON.stringify({ x: 5 }); // '{"x":5}' JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)) // '"2006-01-02T15:04:05.000Z"' JSON.stringify({ x: 5, y: 6 }); // '{"x":5,"y":6}' JSON.stringify(\[new Number(3), new String('false'), new Boolean(false)\]); // '\[3,"false",false\]' // String-keyed array elements are not enumerable and make no sense in JSON let a = \['foo', 'bar'\]; a\['baz'\] = 'quux'; // a: \[ 0: 'foo', 1: 'bar', baz: 'quux' \] JSON.stringify(a); // '\["foo","bar"\]' JSON.stringify({ x: \[10, undefined, function(){}, Symbol('')\] }); // '{"x":\[10,null,null,null\]}' // Standard data structures JSON.stringify(\[new Set(\[1\]), new Map(\[\[1, 2\]\]), new WeakSet(\[{a: 1}\]), new WeakMap(\[\[{a: 1}, 2\]\])\]); // '\[{},{},{},{}\]' // TypedArray JSON.stringify(\[new Int8Array(\[1\]), new Int16Array(\[1\]), new Int32Array(\[1\])\]); // '\[{"0":1},{"0":1},{"0":1}\]' JSON.stringify(\[new Uint8Array(\[1\]), new Uint8ClampedArray(\[1\]), new Uint16Array(\[1\]), new Uint32Array(\[1\])\]); // '\[{"0":1},{"0":1},{"0":1},{"0":1}\]' JSON.stringify(\[new Float32Array(\[1\]), new Float64Array(\[1\])\]); // '\[{"0":1},{"0":1}\]' // toJSON() JSON.stringify({ x: 5, y: 6, toJSON(){ return this.x + this.y; } }); // '11' // Symbols: JSON.stringify({ x: undefined, y: Object, z: Symbol('') }); // '{}' JSON.stringify({ \[Symbol('foo')\]: 'foo' }); // '{}' JSON.stringify({ \[Symbol.for('foo')\]: 'foo' }, \[Symbol.for('foo')\]); // '{}' JSON.stringify({ \[Symbol.for('foo')\]: 'foo' }, function(k, v) { if (typeof k === 'symbol') { return 'a symbol'; } }); // undefined // Non-enumerable properties: JSON.stringify( Object.create(null, { x: { value: 'x', enumerable: false }, y: { value: 'y', enumerable: true } }) ); // '{"y":"y"}' // BigInt values throw JSON.stringify({x: 2n}); // TypeError: BigInt value can't be serialized in JSON](https://hashnode.com/posts/pretty-print-using-jsonstringify/5fe21f8bc65ad56bef3a2f96/comment/5ffbde41a538d00135156397)

Comment · Article · Jan 11, 2021 · [Pretty print using JSON.stringify()](https://hashnode.com/posts/pretty-print-using-jsonstringify/5fe21f8bc65ad56bef3a2f96)

[ES2019 introduced the Array.prototype.flat() method that creates a new array with all the elements of the subarrays concatenated to it recursively up to a specified depth. The following shows the syntax of the flat() method: let newArray = arrayObject.flat(\[depth\]) let arr = \[\[1, 2\],\[3, 4, \[5, 6 \[7, 8\], 9\]\],\[10 \[11, 12\]\]\]; // since the default depth is 1 // only the first level arrays in the parent array are flattend // you would notice that there's a second level nesting after the number '4', in the parent array arr.flat(); // \[ 1, 2, 3, 4, \[ 5, 6, \[ 7, 8 \], 9 \], 10, \[ 11, 12 \] \] // we supplied 2 as the depth arr.flat(2); // \[ 1, 2, 3, 4, 5, 6, \[ 7, 8 \], 9, 10, 11, 12 \] // using 'Infinty' as an argument means flatten all levels to a one-dimensional array arr.flat(Infinity); // \[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 \] // the flat() method also remove empty slots in the array, otherwise called 'array holes' let arrrayHole = \[1, 2, , 4, 5\]; arrayHole.flat(); // \[1, 2, 4, 5\]](https://hashnode.com/posts/arrayflat/5feb6a27b2cf2458fe323505/comment/5ff7fc7daa5c467fe15570ef)

Comment · Article · Jan 8, 2021 · [Array.flat()](https://hashnode.com/posts/arrayflat/5feb6a27b2cf2458fe323505)

[In modern browsers you can do: Array.isArray(obj) For backward compatibility you can add the following: // only implement if no native implementation is available if (typeof Array.isArray === 'undefined') { Array.isArray = function(obj) { return Object.prototype.toString.call(obj) === '\[object Array\]'; } };](https://hashnode.com/posts/how-to-check-if-something-is-an-array-in-javascript/5fe0d1e77cd4e55d1d465e6b/comment/5ff29cf955aabd0578224a12)

Comment · Article · Jan 4, 2021 · [🔥 How to check if something is an array in JavaScript](https://hashnode.com/posts/how-to-check-if-something-is-an-array-in-javascript/5fe0d1e77cd4e55d1d465e6b)
