user1826795
user1826795

Reputation: 195

Javascript multiple values in variable

Is there any way to have one variable with multiple values like this:

var variable = 1, 2, 3;
var enteredVal = 1;

if (enteredVal == variable){
    alert('You chose the right number');
}

So if the variable enteredVal is equal to 1, 2 or 3, it will alert the message. I can't seem to get my head around it.

Upvotes: 6

Views: 64429

Answers (6)

PhiAgent
PhiAgent

Reputation: 158

You can store values of the variable as keys in an object. eg:

let variable={
  1:0,
  2:0,
  3:0
}

enteredVal=3

if (enteredVal in variable) {
  alert('You chose the right number');
}

Upvotes: 1

ajp15243
ajp15243

Reputation: 7952

There is no way to assign multiple distinct values to a single variable.

An alternative is to have variable be an Array, and you can check to see if enteredval is in the array.

var variable = [1, 2, 3];
var enteredval = 1;

if (variable.indexOf(enteredval) > -1){
    alert('you chose the right number');
}

Note that indexOf on an array is not usable in IE8 and below (see the Requirements section at the bottom). In that case you would need to use a framework/library's method, or write it yourself:

var variable = [1, 2, 3];
var enteredval = 1;

for (var i = 0; i < variable.length; i++) {
    if (variable[i] === enteredval) {
        alert('you chose the right number');
        break; // No need to check all the other values in variable
    }
}

To modify arrays after you have instantiated them, take a look at push, pop, shift, and unshift for adding/removing values. For modifying existing values, you can directly access the index and reassign the value.

variable[1] = 5;
// variable is now [1, 5, 3] since arrays are 0-indexed

Upvotes: 11

Bucket
Bucket

Reputation: 7521

Try using an array:

var variables = [1, 2, 3];

You can access members of the array with variables[i], where i is the index of the array, starting at 0.

variables[0] returns 1

variables[1] returns 2

variables[2] returns 3

This way you can use a for loop to iterate through the array:

var enteredval = 1; //your guess
for(var i = 0; i < variables.length; i++) { //go through each element
    if(variables[i] == enteredval) { //if they match...
        alert("You chose the right number!"); //...then you got it!
    }
}

Upvotes: 1

You can implement the indexof function of a string.

var variable = '1,2,3';
    var enteredval = '1';

    if (variable.indexOf(enteredval) > -1) { alert('you choosed the right number'); };

Upvotes: 0

Sean Vieira
Sean Vieira

Reputation: 159905

You can store them in an array and use indexOf (as long as you don't need IE 8 support):

var supportedOptions = [1, 2, 3],
    enteredVal = 1;

if(supportedOptions.indexOf(enteredVal) !== -1) alert("Yep!");

Upvotes: 1

What have you tried
What have you tried

Reputation: 11148

You are going to want to create an array which can hold multiple values. You would then want to use index of to determine if the value is found in the array.

var myArray = new Array();

myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;

Check the value:

if(myArray.indexOf(1) > -1){
// exists
}

Upvotes: 0

Related Questions