Reputation: 137
I have an array named currentOrders
.
if its length is 1 then I want to set the variable output to true if not false using javascript.
I writing something like this using ternary operator
const isOneOrder = currentOrders.length === 1 ? true : false;
I think the above can be rewritten still more good. but i am not sure how it can be rewritten.
How can I rewrite this. could someone help me with this? thanks.
Upvotes: 0
Views: 1041
Reputation: 16908
You can completely remove the ternary operator:
let currentOrders = [1];
const isOneOrder = currentOrders.length === 1;
console.log(isOneOrder);
currentOrders = [1, 2];
const isNotOneOrder = currentOrders.length === 1;
console.log(isNotOneOrder);
Upvotes: 1
Reputation: 6269
you could write it like this
const isOneOrder = !!currentOrders.length;
when you add two !!
it will convert the value to boolean
Upvotes: 1
Reputation: 10204
Simply you can write like this.
const isOneOrder = currentOrders.length === 1;
===
operator will return true
if value and type are same and will return false
in othercases.
Therefore, no need to define true
or false
.
Upvotes: 4