0

Is it possible (in a short way) to check if:

var name = "Nora";

Exists in:

var names:Array = ["Mary", "Porter", "Nora", "Flint", "Elsa", "Clair",...];

Thanks.

2 Answers 2

5

Try .indexOf()

if( names.indexOf( name ) > -1 )
{
    // Success
}
Sign up to request clarification or add additional context in comments.

1 Comment

So I can use if (names.indexOf( name )<0). Many thanks Cherniv, indeed.
3

Use array.indexOf(value).

if (names.indexOf(name) != -1)
{
    // Exist
}
else
{
    // Not exist
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.