DEV Community

Cover image for Assignment has a return value in JS
icncsx
icncsx

Posted on

Assignment has a return value in JS

console.log(b=1) // 1
Enter fullscreen mode Exit fullscreen mode

Because assignment has a return value, we are able to chain assignments.

let a;
let b;

a = b = 1

console.log(a); // 1
console.log(b); // 1
Enter fullscreen mode Exit fullscreen mode

Hard to read, but it works...

let a = 1;
let b = 2;

let c = 3 - (a = b + 1);

console.log(a); // 3
console.log(c); // 0
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
arpple profile image
Arpple

I think it's for REPL, so it will show the final result for assignment as well

> a = 1 + 2
3
Enter fullscreen mode Exit fullscreen mode