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













1















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









share|improve this question






















  • 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















1















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









share|improve this question






















  • 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













1












1








1








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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












1 Answer
1






active

oldest

votes


















2














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)








share|improve this answer




















  • 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











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
);



);













draft saved

draft discarded


















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









2














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)








share|improve this answer




















  • 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
















2














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)








share|improve this answer




















  • 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














2












2








2







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)








share|improve this answer















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)






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 2:51

























answered Mar 8 at 2:43









Code ManiacCode Maniac

10.1k2733




10.1k2733







  • 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













  • 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








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




















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme