Declaring multiple variables in JavaScript

后端 未结 17 914
时光说笑
时光说笑 2020-11-22 13:16

In JavaScript, it is possible to declare multiple variables like this:

var variable1 = "Hello, World!";
var variable2 = "Testing...";
var          


        
17条回答
  •  遥遥无期
    2020-11-22 13:21

    Use the ES6 destructuring assignment: It will unpack values from arrays, or properties from objects, into distinct variables.

    let [variable1 , variable2, variable3] =
    ["Hello, World!", "Testing...", 42];
    
    console.log(variable1); // Hello, World!
    console.log(variable2); // Testing...
    console.log(variable3); // 42

提交回复
热议问题