Posts

Showing posts with the label Javascript

Javascript Hoisting

JavaScript hoisting is a mechanism where variable and function declarations are moved to the top of their respective scopes (either the global scope or function scope) during the compilation or interpretation phase, regardless of where they are declared in the source code. This means that even if a variable or function is declared later in the code, it can be used before its actual declaration. However, it is important to note that only the declaration is hoisted, not the initialization or assignment. In other words, if a variable is initialized after its declaration, its value will not be available until after the initialization. Similarly, if a function expression is used, it will not be hoisted, only function declarations are hoisted. JavaScript hoisting can have some advantages and disadvantages. Advantages: Flexibility : Hoisting provides developers with the flexibility to declare functions or variables anywhere in t...

Create function to convert from string to Integer using javascript

Create a function to convert a string to integer using JavaScript. Javascript function parseStringToInteger(inputValue: string): Number { let value: number; if (inputValue === '0' || Number(inputValue) === 0) { value = 0; } else if (inputValue === '') { value = null; } else { value = Number(inputValue); } return value; } How to use the above function to get the expected result :  Javascript const value = parseStringToInteger("25"); console.log(value); Output : 25

Remove duplicate objects/data from an array of objects in Javascript

How to Remove duplicate object data from an array using JavaScript. To remove the duplicate object data from an array, have to use the filter function. So, it will filter out the duplicate objects/data from an array. Please see the below example. Javascript var uniqueItems = []; var sampleData = [ { id: 1, name: 'firstname' }, { id: 2, name: 'middlename' }, { id: 1, name: 'firstname' }, { id: 1, name: 'lastname' } ] var duplicateItems = sampleData.filter(filteredData => { if (uniqueItems.find(items => items.id === filteredData.id)) { return true; } uniqueItems.push(filteredData); return false; });