3

Possible Duplicate:
How to tell if a string contains a certain character in javascript?

Suppose I have a string in variable ex. var name="Stackoverflow". I want to check in this string if 'z' is exist or not? How can I check this? I don't want to find index or anything else I just want to check if value z is exist or not.

Suppose with code. I have a variable.

var deleteboxvalue = "1111111111111111111111"; if(!deleteboxvalue.indexOf('z') >= 0){ alert("0 not exist"); return false; } 
0

1 Answer 1

9

You can use indexOf like this:

var name = "Stackoverflow" var charExists = (name.indexOf('z') >= 0) ? true : false; alert(charExists); 

Or just (as pointed out by @Felix Kling):

var charExists = (name.indexOf('z') >= 0); 
Sign up to request clarification or add additional context in comments.

3 Comments

Or just var charExists = (name.indexOf('z') >= 0);
@FelixKling: That's nice short way of doing it. Thanks
I have modified the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.