Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

กำหนดค่าเดียวกันให้หลายตัวแปรใน JavaScript

ใช้ Chained Assignment: a = b = c = value

ตัวอย่างโค้ด

var first, second, third, fourth, fifth;
first = second = third = fourth = fifth = 100;

console.log(first);
console.log(second);
console.log(third);
console.log(fourth);
console.log(fifth);
console.log("The sum of all values=" + (first + second + third + fourth + fifth));

วิธีรันโปรแกรม

ใช้คำสั่ง node fileName.js (เช่น node demo114.js)

ผลลัพธ์

100
100
100
100
100
The sum of all values=500