What is the ‘new’ keyword in JavaScript?

It does 5 things: It creates a new object. The type of this object is simply object. It sets this new object’s internal, inaccessible, [[prototype]] (i.e. __proto__) property to be the constructor function’s external, accessible, prototype object (every function object automatically has a prototype property). It makes the this variable point to the newly created object. It executes the constructor function, using the newly created … Read more

What’s the best way to convert a number to a string in JavaScript?

like this: Actually, even though I typically do it like this for simple convenience, over 1,000s of iterations it appears for raw speed there is an advantage for .toString() See Performance tests here (not by me, but found when I went to write my own): http://jsben.ch/#/ghQYR Fastest based on the JSPerf test above: str = num.toString(); It should be … Read more

What does this symbol mean in JavaScript?

See the documentation on MDN about expressions and operators and statements. Basic keywords and general expressions this keyword: How does the “this” keyword work? var x = function() vs. function x() — Function declaration syntax var functionName = function() {} vs function functionName() {} (function(){…})() — IIFE (Immediately Invoked Function Expression) What is the purpose?, How is it called? Why does (function(){…})(); work but function(){…}(); doesn’t? (function(){…})(); vs (function(){…}()); shorter alternatives: !function(){…}(); – What … Read more

How to find the sum of an array of numbers

Recommended (reduce with default value) Array.prototype.reduce can be used to iterate through the array, adding the current element value to the sum of the previous element values.  Run code snippetExpand snippet Without default value You get a TypeError  Run code snippetExpand snippet Prior to ES6’s arrow functions  Run code snippetExpand snippet Non-number inputs If non-numbers are … Read more

Setting “checked” for a checkbox with jQuery

Modern jQuery Use .prop(): DOM API If you’re working with just one element, you can always just access the underlying HTMLInputElement and modify its .checked property: The benefit to using the .prop() and .attr() methods instead of this is that they will operate on all matched elements. jQuery 1.5.x and below The .prop() method is not available, so you need to use .attr(). Note that this is the … Read more

‘react-scripts’ is not recognized as an internal or external command

I’ve got a maven project, within which is JavaScript project cloned as a git sub-module. So the directory structure looks like mavenapp/src/main/javascript/[npm project files] Inside my package.json, the test looks like this: but when I try to run npm test, it says ‘react-scripts’ is not recognized as an internal or external command, Interestingly, when I clone the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)