I want this but in fewer lines using arrows
console.log([1, 2, 3, 4].every(
value => {
if (value > 0) {
return 1
}
}))
output: true
It is working fine but when I tried to reduce the lines as below code it is not working is there any way to do it, is it not possible to write a single if statement without using any ternary
console.log([1, 2, 3, 4].every(value =>
if (value > 0) 1)
);
output : /tmp/main.js:1
console.log([1,2,3,4].every(value=> if(value>0) 1))
^^
SyntaxError: Unexpected token 'if'
at Module._compile (internal/modules/cjs/loader.js:892:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11
[Program exited with exit code 1]
Is it possible to return anything apart from true or false using arrow operator like this? I want to return awesome but it is throwing me an error.
k=val=>val>0 "awesome";
console.log(k(2))
k=val=>val>0 "awesome";
^^^^^^^^^
SyntaxError: Unexpected string
at Module._compile (internal/modules/cjs/loader.js:892:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11
I tried to ask this question which is based on arrow function but someone told me to ask the question separately, that's why I'm asking here.