How to make a variable with a list of multiple values

Viewed 75

What I'm trying to do is make a variable that has more than 1 value. so if someone types either "value1" or "value2" it'll return as the variable.

my script for this portion is:

var multiply = "x", "*"

it returns:

var multiply = "x", "*"
                    ^^^

Syntax Error: Unexpected String

is it possible for me to do this, or what is the correct way?

4 Answers

There are two [easy] ways to do this.

  1. You can use Arrays

let multiply = ["x","*"];
console.log(multiply);
console.log(multiply[0], multiply[1]);

  1. you can use an object

let multiply = {
  value1:"x",
  value2:"*",
};
console.log(multiply);
console.log(multiply.value1, multiply.value2);

The second option is *closer* to what you were asking for with the user typing value1 and value2, however as seen they must prefix it with "multiply."

You can write like this

let val = "x";
var multiply = val || "*";
console.log(multiply); // output will be x

let val = "";
var multiply = val || "*";
console.log(multiply); // output will be *

If we want to store multiple values into variable then we should be using array

var list = ["x", "*"]

Use arrays to store multiple values in a single variable.

var list = ["x", "*"]

and you can call the value using indices.

list[0] will return "x" and likewise list[1] returns "*"

Related