ES6 .some() with the ternary operator behaves differentlyArrow function without curly bracesWhich equals operator (== vs ===) should be used in JavaScript comparisons?Difference between == and === in JavaScriptWhat's the difference between using “let” and “var”?What is the !! (not not) operator in JavaScript?What is the difference between call and apply?Differences between lodash and underscoreWhat is the difference between Bower and npm?What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?What's the difference between tilde(~) and caret(^) in package.json?Using Node.js require vs. ES6 import/export
15% tax on $7.5k earnings. Is that right?
Strong empirical falsification of quantum mechanics based on vacuum energy density
Yosemite Fire Rings - What to Expect?
Picking the different solutions to the time independent Schrodinger eqaution
How could a planet have erratic days?
Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?
Fear of getting stuck on one programming language / technology that is not used in my country
How to hide some fields of struct in C?
Terse Method to Swap Lowest for Highest?
Can I still be respawned if I die by falling off the map?
Open a doc from terminal, but not by its name
How to cover method return statement in Apex Class?
Can a Canadian Travel to the USA twice, less than 180 days each time?
Why is this estimator biased?
When were female captains banned from Starfleet?
Extract more than nine arguments that occur periodically in a sentence to use in macros in order to typset
Title 53, why is it reserved?
What is Cash Advance APR?
Why is it that I can sometimes guess the next note?
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
Is there a RAID 0 Equivalent for RAM?
How to explain what's wrong with this application of the chain rule?
Why "had" in "[something] we would have made had we used [something]"?
Why would a new[] expression ever invoke a destructor?
ES6 .some() with the ternary operator behaves differently
Arrow function without curly bracesWhich equals operator (== vs ===) should be used in JavaScript comparisons?Difference between == and === in JavaScriptWhat's the difference between using “let” and “var”?What is the !! (not not) operator in JavaScript?What is the difference between call and apply?Differences between lodash and underscoreWhat is the difference between Bower and npm?What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?What's the difference between tilde(~) and caret(^) in package.json?Using Node.js require vs. ES6 import/export
I've noticed a weird behavior with .some() array method and a ternary operator.
It behaves differently when the integer(count) has to be incremented on each true case with and without the curly brackets.
Although, the console.log shows correct truthy on each iteration.
Any thoughts?
> let arr = ['011','202','3300']
undefined
> let count = 0;
undefined
> arr.some(k => k.includes('0') ? count++ : null);
true
> count;
2 // is not correct
> count = 0;
0
> arr.some(k => k.includes('0') ? count++ : null);
false
> count;
3 // correct
>
> arr.some(k => k.includes('0') ? console.log('true') : null);
true
true
true
false
> arr.some(k => k.includes('0') ? console.log('true') : null);
true
true
true
false
javascript arrays node.js ecmascript-6
add a comment |
I've noticed a weird behavior with .some() array method and a ternary operator.
It behaves differently when the integer(count) has to be incremented on each true case with and without the curly brackets.
Although, the console.log shows correct truthy on each iteration.
Any thoughts?
> let arr = ['011','202','3300']
undefined
> let count = 0;
undefined
> arr.some(k => k.includes('0') ? count++ : null);
true
> count;
2 // is not correct
> count = 0;
0
> arr.some(k => k.includes('0') ? count++ : null);
false
> count;
3 // correct
>
> arr.some(k => k.includes('0') ? console.log('true') : null);
true
true
true
false
> arr.some(k => k.includes('0') ? console.log('true') : null);
true
true
true
false
javascript arrays node.js ecmascript-6
You need to know about fat arrow.... That is your issue. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… And some() is the wrong thing to be using here.... some() is checking to see if one thing is true.... again, read the documentation for some()
– epascarello
Mar 8 at 2:40
Possible duplicate of Arrow function without curly braces
– david
Mar 8 at 2:46
add a comment |
I've noticed a weird behavior with .some() array method and a ternary operator.
It behaves differently when the integer(count) has to be incremented on each true case with and without the curly brackets.
Although, the console.log shows correct truthy on each iteration.
Any thoughts?
> let arr = ['011','202','3300']
undefined
> let count = 0;
undefined
> arr.some(k => k.includes('0') ? count++ : null);
true
> count;
2 // is not correct
> count = 0;
0
> arr.some(k => k.includes('0') ? count++ : null);
false
> count;
3 // correct
>
> arr.some(k => k.includes('0') ? console.log('true') : null);
true
true
true
false
> arr.some(k => k.includes('0') ? console.log('true') : null);
true
true
true
false
javascript arrays node.js ecmascript-6
I've noticed a weird behavior with .some() array method and a ternary operator.
It behaves differently when the integer(count) has to be incremented on each true case with and without the curly brackets.
Although, the console.log shows correct truthy on each iteration.
Any thoughts?
> let arr = ['011','202','3300']
undefined
> let count = 0;
undefined
> arr.some(k => k.includes('0') ? count++ : null);
true
> count;
2 // is not correct
> count = 0;
0
> arr.some(k => k.includes('0') ? count++ : null);
false
> count;
3 // correct
>
> arr.some(k => k.includes('0') ? console.log('true') : null);
true
true
true
false
> arr.some(k => k.includes('0') ? console.log('true') : null);
true
true
true
false
javascript arrays node.js ecmascript-6
javascript arrays node.js ecmascript-6
asked Mar 8 at 2:35
Sgryt87Sgryt87
345
345
You need to know about fat arrow.... That is your issue. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… And some() is the wrong thing to be using here.... some() is checking to see if one thing is true.... again, read the documentation for some()
– epascarello
Mar 8 at 2:40
Possible duplicate of Arrow function without curly braces
– david
Mar 8 at 2:46
add a comment |
You need to know about fat arrow.... That is your issue. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… And some() is the wrong thing to be using here.... some() is checking to see if one thing is true.... again, read the documentation for some()
– epascarello
Mar 8 at 2:40
Possible duplicate of Arrow function without curly braces
– david
Mar 8 at 2:46
You need to know about fat arrow.... That is your issue. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… And some() is the wrong thing to be using here.... some() is checking to see if one thing is true.... again, read the documentation for some()
– epascarello
Mar 8 at 2:40
You need to know about fat arrow.... That is your issue. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… And some() is the wrong thing to be using here.... some() is checking to see if one thing is true.... again, read the documentation for some()
– epascarello
Mar 8 at 2:40
Possible duplicate of Arrow function without curly braces
– david
Mar 8 at 2:46
Possible duplicate of Arrow function without curly braces
– david
Mar 8 at 2:46
add a comment |
1 Answer
1
active
oldest
votes
Let's understand this
Why this one is giving output 2
arr.some(k => k.includes('0') ? count++ : null);
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
So on first iteration
count++
will return 0 and than increment value by 1. ( since it is post increment )On second iteration it will return value as
1
which is true and than increment by 1. ( since you found one true value some will stop iteration )
Why this one is giving output 3
arr.some(k => k.includes('0') ? console.log('true') : null);
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
- Here you're not utilizing implicit return of arrow function so on each iteration you're eventually returning undefined. so your some will iterate through all the elements and you get output as 3.
Just add a return statement and see the changes.
let count = 0;
let arr = ['011','202','3300']
arr.some(k => return k.includes('0') ? count++ : null);
console.log(count)
1
Could add that withinnothing is being returned, hence always
false
. Apparently you read minds since you just added that
– Neil Lunn
Mar 8 at 2:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55055922%2fes6-some-with-the-ternary-operator-behaves-differently%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let's understand this
Why this one is giving output 2
arr.some(k => k.includes('0') ? count++ : null);
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
So on first iteration
count++
will return 0 and than increment value by 1. ( since it is post increment )On second iteration it will return value as
1
which is true and than increment by 1. ( since you found one true value some will stop iteration )
Why this one is giving output 3
arr.some(k => k.includes('0') ? console.log('true') : null);
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
- Here you're not utilizing implicit return of arrow function so on each iteration you're eventually returning undefined. so your some will iterate through all the elements and you get output as 3.
Just add a return statement and see the changes.
let count = 0;
let arr = ['011','202','3300']
arr.some(k => return k.includes('0') ? count++ : null);
console.log(count)
1
Could add that withinnothing is being returned, hence always
false
. Apparently you read minds since you just added that
– Neil Lunn
Mar 8 at 2:45
add a comment |
Let's understand this
Why this one is giving output 2
arr.some(k => k.includes('0') ? count++ : null);
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
So on first iteration
count++
will return 0 and than increment value by 1. ( since it is post increment )On second iteration it will return value as
1
which is true and than increment by 1. ( since you found one true value some will stop iteration )
Why this one is giving output 3
arr.some(k => k.includes('0') ? console.log('true') : null);
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
- Here you're not utilizing implicit return of arrow function so on each iteration you're eventually returning undefined. so your some will iterate through all the elements and you get output as 3.
Just add a return statement and see the changes.
let count = 0;
let arr = ['011','202','3300']
arr.some(k => return k.includes('0') ? count++ : null);
console.log(count)
1
Could add that withinnothing is being returned, hence always
false
. Apparently you read minds since you just added that
– Neil Lunn
Mar 8 at 2:45
add a comment |
Let's understand this
Why this one is giving output 2
arr.some(k => k.includes('0') ? count++ : null);
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
So on first iteration
count++
will return 0 and than increment value by 1. ( since it is post increment )On second iteration it will return value as
1
which is true and than increment by 1. ( since you found one true value some will stop iteration )
Why this one is giving output 3
arr.some(k => k.includes('0') ? console.log('true') : null);
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
- Here you're not utilizing implicit return of arrow function so on each iteration you're eventually returning undefined. so your some will iterate through all the elements and you get output as 3.
Just add a return statement and see the changes.
let count = 0;
let arr = ['011','202','3300']
arr.some(k => return k.includes('0') ? count++ : null);
console.log(count)
Let's understand this
Why this one is giving output 2
arr.some(k => k.includes('0') ? count++ : null);
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
So on first iteration
count++
will return 0 and than increment value by 1. ( since it is post increment )On second iteration it will return value as
1
which is true and than increment by 1. ( since you found one true value some will stop iteration )
Why this one is giving output 3
arr.some(k => k.includes('0') ? console.log('true') : null);
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
- Here you're not utilizing implicit return of arrow function so on each iteration you're eventually returning undefined. so your some will iterate through all the elements and you get output as 3.
Just add a return statement and see the changes.
let count = 0;
let arr = ['011','202','3300']
arr.some(k => return k.includes('0') ? count++ : null);
console.log(count)
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
let count = 0;
let arr = ['011','202','3300']
arr.some(k => k.includes('0') ? count++ : null);
console.log(count)
let count = 0;
let arr = ['011','202','3300']
arr.some(k => return k.includes('0') ? count++ : null);
console.log(count)
let count = 0;
let arr = ['011','202','3300']
arr.some(k => return k.includes('0') ? count++ : null);
console.log(count)
edited Mar 8 at 2:51
answered Mar 8 at 2:43
Code ManiacCode Maniac
10.1k2733
10.1k2733
1
Could add that withinnothing is being returned, hence always
false
. Apparently you read minds since you just added that
– Neil Lunn
Mar 8 at 2:45
add a comment |
1
Could add that withinnothing is being returned, hence always
false
. Apparently you read minds since you just added that
– Neil Lunn
Mar 8 at 2:45
1
1
Could add that within
nothing is being returned, hence always false
. Apparently you read minds since you just added that– Neil Lunn
Mar 8 at 2:45
Could add that within
nothing is being returned, hence always false
. Apparently you read minds since you just added that– Neil Lunn
Mar 8 at 2:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55055922%2fes6-some-with-the-ternary-operator-behaves-differently%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You need to know about fat arrow.... That is your issue. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… And some() is the wrong thing to be using here.... some() is checking to see if one thing is true.... again, read the documentation for some()
– epascarello
Mar 8 at 2:40
Possible duplicate of Arrow function without curly braces
– david
Mar 8 at 2:46