Please explain this returnNumber function [duplicate] - javascript

If I use:
1.09 * 1; // returns "1.09"
But if I use:
1,09 * 1; // returns "9"
I know that 1,09 isn't a number.
What does the comma do in the last piece of code?
More Examples
if (0,9) alert("ok"); // alert
if (9,0) alert("ok"); // don't alert
alert(1); alert(2); alert(3); // 3 alerts
alert(1), alert(2), alert(3); // 3 alerts too
alert("2",
foo = function (param) {
alert(param)
},
foo('1')
)
foo('3'); // alerts 1, 2 and 3

The comma operator evaluates both of
its operands (from left to right) and
returns the value of the second
operand.
Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
For example, the expression 1,2,3,4,5 evaluates to 5. Obviously the comma operator is useful only for operations with side-effects.
console.log(1,2,3,4,5);
console.log((1,2,3,4,5));

Some more to consider:
console.log((0, 9));
console.log((9, 0));
console.log(("foo", "bar"));

The specific syntax allows you to functionally bake bread and hand it to the customer to consume with no returns.
(new Array(3)).fill()
.map(()=>({state:"dough", bake(){this.state="baked"}, consume(){this.state="consumed"}}))
.map(bread=>(console.log(`Adding ${bread.state} to oven.`), bread.bake(), bread))
.map(bread=>(console.log(`Consuming ${bread.state} bread.`), bread.consume(), bread))
.map(bread=>console.log(`Bread is now ${bread.state}.`))
Adding dough to oven.
Adding dough to oven.
Adding dough to oven.
Consuming baked bread.
Consuming baked bread.
Consuming baked bread.
Bread is now consumed.
Bread is now consumed.
Bread is now consumed.

Adding/modifying properties to an object and returning it in the same line is a possible use-case:
console.log(
((x) => (o = {biggerCond: r => r >= x},
o.r5 = Array.from(window.crypto.getRandomValues(new Uint16Array(5))),
o.isAnyBigger = o.r5.some(o.biggerCond),
o.bigger = o.isAnyBigger ? o.r5.filter(o.biggerCond) : [x], o )
)(5e4)
);
// Example
// {
// bigger: [58414, 56500, 63397],
// isAnyBigger: true,
// isBiggerCond: r => r >= x,
// r5: [58414, 12015, 56500, 63397, 43861]
// }
The above anonymous function returns an object with random values bigger than the input value or, if there's none, with the input value itself in an array in contained in the bigger property.
It is still syntactic sugar (like arrow functions), but it does shorten the number of lines... I wonder if some JS minifiers detect and adjust the code in a similar way automatically. Run it in your console:
((x)=>(o={biggerCond:r=>r>=x},o.r5=Array.from(window.crypto.getRandomValues(new Uint16Array(5))),o.isAnyBigger=o.r5.some(o.biggerCond),o.bigger=o.isAnyBigger?o.r5.filter(o.biggerCond):[x],o))(5e4)

Have a look here - the comma stands for multiple expressions / statements. For example in your code you could use a line like this:
var a=0, b=0, c=0;
This would declare all three variables without writing:
var a=0;
var b=0;
var c=0;
Hope that helps.

Related

how works a variable with multiple strings [duplicate]

If I use:
1.09 * 1; // returns "1.09"
But if I use:
1,09 * 1; // returns "9"
I know that 1,09 isn't a number.
What does the comma do in the last piece of code?
More Examples
if (0,9) alert("ok"); // alert
if (9,0) alert("ok"); // don't alert
alert(1); alert(2); alert(3); // 3 alerts
alert(1), alert(2), alert(3); // 3 alerts too
alert("2",
foo = function (param) {
alert(param)
},
foo('1')
)
foo('3'); // alerts 1, 2 and 3
The comma operator evaluates both of
its operands (from left to right) and
returns the value of the second
operand.
Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
For example, the expression 1,2,3,4,5 evaluates to 5. Obviously the comma operator is useful only for operations with side-effects.
console.log(1,2,3,4,5);
console.log((1,2,3,4,5));
Some more to consider:
console.log((0, 9));
console.log((9, 0));
console.log(("foo", "bar"));
The specific syntax allows you to functionally bake bread and hand it to the customer to consume with no returns.
(new Array(3)).fill()
.map(()=>({state:"dough", bake(){this.state="baked"}, consume(){this.state="consumed"}}))
.map(bread=>(console.log(`Adding ${bread.state} to oven.`), bread.bake(), bread))
.map(bread=>(console.log(`Consuming ${bread.state} bread.`), bread.consume(), bread))
.map(bread=>console.log(`Bread is now ${bread.state}.`))
Adding dough to oven.
Adding dough to oven.
Adding dough to oven.
Consuming baked bread.
Consuming baked bread.
Consuming baked bread.
Bread is now consumed.
Bread is now consumed.
Bread is now consumed.
Adding/modifying properties to an object and returning it in the same line is a possible use-case:
console.log(
((x) => (o = {biggerCond: r => r >= x},
o.r5 = Array.from(window.crypto.getRandomValues(new Uint16Array(5))),
o.isAnyBigger = o.r5.some(o.biggerCond),
o.bigger = o.isAnyBigger ? o.r5.filter(o.biggerCond) : [x], o )
)(5e4)
);
// Example
// {
// bigger: [58414, 56500, 63397],
// isAnyBigger: true,
// isBiggerCond: r => r >= x,
// r5: [58414, 12015, 56500, 63397, 43861]
// }
The above anonymous function returns an object with random values bigger than the input value or, if there's none, with the input value itself in an array in contained in the bigger property.
It is still syntactic sugar (like arrow functions), but it does shorten the number of lines... I wonder if some JS minifiers detect and adjust the code in a similar way automatically. Run it in your console:
((x)=>(o={biggerCond:r=>r>=x},o.r5=Array.from(window.crypto.getRandomValues(new Uint16Array(5))),o.isAnyBigger=o.r5.some(o.biggerCond),o.bigger=o.isAnyBigger?o.r5.filter(o.biggerCond):[x],o))(5e4)
Have a look here - the comma stands for multiple expressions / statements. For example in your code you could use a line like this:
var a=0, b=0, c=0;
This would declare all three variables without writing:
var a=0;
var b=0;
var c=0;
Hope that helps.

Statements inside of Conditionals (if) [duplicate]

If I use:
1.09 * 1; // returns "1.09"
But if I use:
1,09 * 1; // returns "9"
I know that 1,09 isn't a number.
What does the comma do in the last piece of code?
More Examples
if (0,9) alert("ok"); // alert
if (9,0) alert("ok"); // don't alert
alert(1); alert(2); alert(3); // 3 alerts
alert(1), alert(2), alert(3); // 3 alerts too
alert("2",
foo = function (param) {
alert(param)
},
foo('1')
)
foo('3'); // alerts 1, 2 and 3
The comma operator evaluates both of
its operands (from left to right) and
returns the value of the second
operand.
Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
For example, the expression 1,2,3,4,5 evaluates to 5. Obviously the comma operator is useful only for operations with side-effects.
console.log(1,2,3,4,5);
console.log((1,2,3,4,5));
Some more to consider:
console.log((0, 9));
console.log((9, 0));
console.log(("foo", "bar"));
The specific syntax allows you to functionally bake bread and hand it to the customer to consume with no returns.
(new Array(3)).fill()
.map(()=>({state:"dough", bake(){this.state="baked"}, consume(){this.state="consumed"}}))
.map(bread=>(console.log(`Adding ${bread.state} to oven.`), bread.bake(), bread))
.map(bread=>(console.log(`Consuming ${bread.state} bread.`), bread.consume(), bread))
.map(bread=>console.log(`Bread is now ${bread.state}.`))
Adding dough to oven.
Adding dough to oven.
Adding dough to oven.
Consuming baked bread.
Consuming baked bread.
Consuming baked bread.
Bread is now consumed.
Bread is now consumed.
Bread is now consumed.
Adding/modifying properties to an object and returning it in the same line is a possible use-case:
console.log(
((x) => (o = {biggerCond: r => r >= x},
o.r5 = Array.from(window.crypto.getRandomValues(new Uint16Array(5))),
o.isAnyBigger = o.r5.some(o.biggerCond),
o.bigger = o.isAnyBigger ? o.r5.filter(o.biggerCond) : [x], o )
)(5e4)
);
// Example
// {
// bigger: [58414, 56500, 63397],
// isAnyBigger: true,
// isBiggerCond: r => r >= x,
// r5: [58414, 12015, 56500, 63397, 43861]
// }
The above anonymous function returns an object with random values bigger than the input value or, if there's none, with the input value itself in an array in contained in the bigger property.
It is still syntactic sugar (like arrow functions), but it does shorten the number of lines... I wonder if some JS minifiers detect and adjust the code in a similar way automatically. Run it in your console:
((x)=>(o={biggerCond:r=>r>=x},o.r5=Array.from(window.crypto.getRandomValues(new Uint16Array(5))),o.isAnyBigger=o.r5.some(o.biggerCond),o.bigger=o.isAnyBigger?o.r5.filter(o.biggerCond):[x],o))(5e4)
Have a look here - the comma stands for multiple expressions / statements. For example in your code you could use a line like this:
var a=0, b=0, c=0;
This would declare all three variables without writing:
var a=0;
var b=0;
var c=0;
Hope that helps.

TwoSum - Integers and Arrays [duplicate]

If I use:
1.09 * 1; // returns "1.09"
But if I use:
1,09 * 1; // returns "9"
I know that 1,09 isn't a number.
What does the comma do in the last piece of code?
More Examples
if (0,9) alert("ok"); // alert
if (9,0) alert("ok"); // don't alert
alert(1); alert(2); alert(3); // 3 alerts
alert(1), alert(2), alert(3); // 3 alerts too
alert("2",
foo = function (param) {
alert(param)
},
foo('1')
)
foo('3'); // alerts 1, 2 and 3
The comma operator evaluates both of
its operands (from left to right) and
returns the value of the second
operand.
Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
For example, the expression 1,2,3,4,5 evaluates to 5. Obviously the comma operator is useful only for operations with side-effects.
console.log(1,2,3,4,5);
console.log((1,2,3,4,5));
Some more to consider:
console.log((0, 9));
console.log((9, 0));
console.log(("foo", "bar"));
The specific syntax allows you to functionally bake bread and hand it to the customer to consume with no returns.
(new Array(3)).fill()
.map(()=>({state:"dough", bake(){this.state="baked"}, consume(){this.state="consumed"}}))
.map(bread=>(console.log(`Adding ${bread.state} to oven.`), bread.bake(), bread))
.map(bread=>(console.log(`Consuming ${bread.state} bread.`), bread.consume(), bread))
.map(bread=>console.log(`Bread is now ${bread.state}.`))
Adding dough to oven.
Adding dough to oven.
Adding dough to oven.
Consuming baked bread.
Consuming baked bread.
Consuming baked bread.
Bread is now consumed.
Bread is now consumed.
Bread is now consumed.
Adding/modifying properties to an object and returning it in the same line is a possible use-case:
console.log(
((x) => (o = {biggerCond: r => r >= x},
o.r5 = Array.from(window.crypto.getRandomValues(new Uint16Array(5))),
o.isAnyBigger = o.r5.some(o.biggerCond),
o.bigger = o.isAnyBigger ? o.r5.filter(o.biggerCond) : [x], o )
)(5e4)
);
// Example
// {
// bigger: [58414, 56500, 63397],
// isAnyBigger: true,
// isBiggerCond: r => r >= x,
// r5: [58414, 12015, 56500, 63397, 43861]
// }
The above anonymous function returns an object with random values bigger than the input value or, if there's none, with the input value itself in an array in contained in the bigger property.
It is still syntactic sugar (like arrow functions), but it does shorten the number of lines... I wonder if some JS minifiers detect and adjust the code in a similar way automatically. Run it in your console:
((x)=>(o={biggerCond:r=>r>=x},o.r5=Array.from(window.crypto.getRandomValues(new Uint16Array(5))),o.isAnyBigger=o.r5.some(o.biggerCond),o.bigger=o.isAnyBigger?o.r5.filter(o.biggerCond):[x],o))(5e4)
Have a look here - the comma stands for multiple expressions / statements. For example in your code you could use a line like this:
var a=0, b=0, c=0;
This would declare all three variables without writing:
var a=0;
var b=0;
var c=0;
Hope that helps.

Multiple return values in JavaScript [duplicate]

If I use:
1.09 * 1; // returns "1.09"
But if I use:
1,09 * 1; // returns "9"
I know that 1,09 isn't a number.
What does the comma do in the last piece of code?
More Examples
if (0,9) alert("ok"); // alert
if (9,0) alert("ok"); // don't alert
alert(1); alert(2); alert(3); // 3 alerts
alert(1), alert(2), alert(3); // 3 alerts too
alert("2",
foo = function (param) {
alert(param)
},
foo('1')
)
foo('3'); // alerts 1, 2 and 3
The comma operator evaluates both of
its operands (from left to right) and
returns the value of the second
operand.
Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
For example, the expression 1,2,3,4,5 evaluates to 5. Obviously the comma operator is useful only for operations with side-effects.
console.log(1,2,3,4,5);
console.log((1,2,3,4,5));
Some more to consider:
console.log((0, 9));
console.log((9, 0));
console.log(("foo", "bar"));
The specific syntax allows you to functionally bake bread and hand it to the customer to consume with no returns.
(new Array(3)).fill()
.map(()=>({state:"dough", bake(){this.state="baked"}, consume(){this.state="consumed"}}))
.map(bread=>(console.log(`Adding ${bread.state} to oven.`), bread.bake(), bread))
.map(bread=>(console.log(`Consuming ${bread.state} bread.`), bread.consume(), bread))
.map(bread=>console.log(`Bread is now ${bread.state}.`))
Adding dough to oven.
Adding dough to oven.
Adding dough to oven.
Consuming baked bread.
Consuming baked bread.
Consuming baked bread.
Bread is now consumed.
Bread is now consumed.
Bread is now consumed.
Adding/modifying properties to an object and returning it in the same line is a possible use-case:
console.log(
((x) => (o = {biggerCond: r => r >= x},
o.r5 = Array.from(window.crypto.getRandomValues(new Uint16Array(5))),
o.isAnyBigger = o.r5.some(o.biggerCond),
o.bigger = o.isAnyBigger ? o.r5.filter(o.biggerCond) : [x], o )
)(5e4)
);
// Example
// {
// bigger: [58414, 56500, 63397],
// isAnyBigger: true,
// isBiggerCond: r => r >= x,
// r5: [58414, 12015, 56500, 63397, 43861]
// }
The above anonymous function returns an object with random values bigger than the input value or, if there's none, with the input value itself in an array in contained in the bigger property.
It is still syntactic sugar (like arrow functions), but it does shorten the number of lines... I wonder if some JS minifiers detect and adjust the code in a similar way automatically. Run it in your console:
((x)=>(o={biggerCond:r=>r>=x},o.r5=Array.from(window.crypto.getRandomValues(new Uint16Array(5))),o.isAnyBigger=o.r5.some(o.biggerCond),o.bigger=o.isAnyBigger?o.r5.filter(o.biggerCond):[x],o))(5e4)
Have a look here - the comma stands for multiple expressions / statements. For example in your code you could use a line like this:
var a=0, b=0, c=0;
This would declare all three variables without writing:
var a=0;
var b=0;
var c=0;
Hope that helps.

Difference between a[0,0] and a[0][0] in array [duplicate]

If I use:
1.09 * 1; // returns "1.09"
But if I use:
1,09 * 1; // returns "9"
I know that 1,09 isn't a number.
What does the comma do in the last piece of code?
More Examples
if (0,9) alert("ok"); // alert
if (9,0) alert("ok"); // don't alert
alert(1); alert(2); alert(3); // 3 alerts
alert(1), alert(2), alert(3); // 3 alerts too
alert("2",
foo = function (param) {
alert(param)
},
foo('1')
)
foo('3'); // alerts 1, 2 and 3
The comma operator evaluates both of
its operands (from left to right) and
returns the value of the second
operand.
Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
For example, the expression 1,2,3,4,5 evaluates to 5. Obviously the comma operator is useful only for operations with side-effects.
console.log(1,2,3,4,5);
console.log((1,2,3,4,5));
Some more to consider:
console.log((0, 9));
console.log((9, 0));
console.log(("foo", "bar"));
The specific syntax allows you to functionally bake bread and hand it to the customer to consume with no returns.
(new Array(3)).fill()
.map(()=>({state:"dough", bake(){this.state="baked"}, consume(){this.state="consumed"}}))
.map(bread=>(console.log(`Adding ${bread.state} to oven.`), bread.bake(), bread))
.map(bread=>(console.log(`Consuming ${bread.state} bread.`), bread.consume(), bread))
.map(bread=>console.log(`Bread is now ${bread.state}.`))
Adding dough to oven.
Adding dough to oven.
Adding dough to oven.
Consuming baked bread.
Consuming baked bread.
Consuming baked bread.
Bread is now consumed.
Bread is now consumed.
Bread is now consumed.
Adding/modifying properties to an object and returning it in the same line is a possible use-case:
console.log(
((x) => (o = {biggerCond: r => r >= x},
o.r5 = Array.from(window.crypto.getRandomValues(new Uint16Array(5))),
o.isAnyBigger = o.r5.some(o.biggerCond),
o.bigger = o.isAnyBigger ? o.r5.filter(o.biggerCond) : [x], o )
)(5e4)
);
// Example
// {
// bigger: [58414, 56500, 63397],
// isAnyBigger: true,
// isBiggerCond: r => r >= x,
// r5: [58414, 12015, 56500, 63397, 43861]
// }
The above anonymous function returns an object with random values bigger than the input value or, if there's none, with the input value itself in an array in contained in the bigger property.
It is still syntactic sugar (like arrow functions), but it does shorten the number of lines... I wonder if some JS minifiers detect and adjust the code in a similar way automatically. Run it in your console:
((x)=>(o={biggerCond:r=>r>=x},o.r5=Array.from(window.crypto.getRandomValues(new Uint16Array(5))),o.isAnyBigger=o.r5.some(o.biggerCond),o.bigger=o.isAnyBigger?o.r5.filter(o.biggerCond):[x],o))(5e4)
Have a look here - the comma stands for multiple expressions / statements. For example in your code you could use a line like this:
var a=0, b=0, c=0;
This would declare all three variables without writing:
var a=0;
var b=0;
var c=0;
Hope that helps.

Categories