if (Expression) Statement else Statement
The conditional expression in an if statement is coerced to a boolean value.
Therefore javascript's falsy values can be used in the right situations to check for missing values.
For example:
if (name) return `Hello, ${name}!`;
else return 'Hello, World!';
or in an expression statement that produces a value:
name ? `Hello, ${name}!` : 'Hello, World!';
Javascript's falsy values are undefined, null, false, 0, NaN, ''
.
Wolfram defines the modulo operation as "m mod n is the remainder on division of m by n. The sign of m mod n for real m, n is always the same sign of n."
Python has a true modulo operator.
-5 % 2
# => 1
5 % -2
# => -1
JavaScript does not have a modulo operator. According to the ECMAScript Specification "The % MultiplicativeOperator yields the remainder of its operands from an implied division; the left operand is the dividend and the right operand is the divisor."
Most importantly "The sign of the result equals the sign of the dividend."
JavaScript has a remainder operator, not a modulo operator.
-5 % 2
// => -1
5 % -2
// => 1
https://github.com/DeveloperToolsWG/console-object/blob/master/api.md#consolelogobject--object-
Specifier | Description |
---|---|
%s |
Formats the value as a string (cooercing via toString() if necessary) |
%d , %i |
Formats the value as an integer |
%f |
Formats the value as a floating point value |
%o |
Formats the value as an expandable DOM Element (or JavaScript Object if it is not) |
%O |
Formats the value as an expandable JavaScript Object |
%c |
Formats the output string according to CSS styles you provide |
var array = [1, 3, 5, 6, 2];
console.log('We have this many elements in %s and the elements are %o', array, array);
//=> We have this many elements in Array[5] and the elements are [1, 3, 5, 6, 2]
The reduce method compresses the elements of an array to a single value, even if that single value is another array of elements.
Array.prototype.reduce( callbackfn [, initialValue])
callbackfn is called with four arguments: the previousValue (value from the previous call to callbackfn), the currentValue (value of the current element), the currentIndex, and the object being traversed
var a = [1, 2, 3, 4, 5];
// return the sum of the elements of the array
a.reduce((prev, cur) => {
return prev + cur;
});
//=> 15
// reduce the array to an object whose sum
// property is the sum of the elements of the array
a.reduce((prev, cur) => {
// give a value of 1 for when the property is non-existent
prev.sum = prev.sum + cur || 1;
return prev;
}, {})
//=> { sum: 15 }
// reduce an array to a single element that is
// the sum of all elements of the array
a.reduce((prev, cur) => {
// give a value of 1 for when the array is empty
prev[0] = prev[0] + cur || 1;
return prev;
}, [])
//=> [15]
JavaScript does not do integer division. The multiplicative /
operator applied to two operands always returns a double precision floating point number. This rule from C will not hold true: If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a;
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
int main(void) {
size_t a = 13;
size_t b = 5;
bool x = (a == (a / b) * b + (a % b));
printf("The result is %s\n", x ? "true" : "false");
return EXIT_SUCCESS;
}
In JavaScript a === (a / b) * b + (a % b)
will return true
only when the result has no fractional part as a remainder. You can replicate integer division only by forcing the quotient to throw away the fractional part.
var a = 13;
var b = 5;
var x = a === (Math.floor(a / b)) * b + (a % b);
console.log('The result is %s', x);