Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8548857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:35:37+00:00 2026-06-11T13:35:37+00:00

This statement simply checks an @ symbol in a textbox. if (string1.indexOf(@)==-1){ alert(Please input

  • 0

This statement simply checks an “@” symbol in a textbox.

if (string1.indexOf("@")==-1){
 alert("Please input a valid email address!")
 document.example.email.focus()

This. if (string1.indexOf("@")==-1){ why do we use -1 instead of 0.

it baffles me, since 0 is been used almost everywhere as a ‘null’ or ’empty’ value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:35:39+00:00Added an answer on June 11, 2026 at 1:35 pm

    In most programming languages, indexes are zero-based, meaning that the first position in an index (again in most programming languages, a string is an index of characters) will be 0 – hence 0 can’t be used to indicate nothing was found anywhere.


    To help clarify:

    A string is an index of chars (characters, or single-symbol types). So, "hi@ho.com" is an index containing 9 positions:

    [0] = 'h'
    [1] = 'i'
    [2] = '@'
    [3] = 'h'
    [4] = 'o'
    [5] = '.'
    [6] = 'c'
    [7] = 'o'
    [8] = 'm'
    

    Because indexes in JavaScript are zero-based they always start with their first position being 0. indexOf uses -1 to tell you that it couldn’t find the @ anywhere because it can’t use 0 since 0 is actually the first position of the index.

    Even if strings weren’t indexes most languages would still use -1 to indicate it couldn’t find the character and 0 to indicate the first position for reasons of tradition and de facto standards.

    In the above example, indexOf("@") would return 2, not 3. Again, because indexes are zero-based.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This statement doesnot work in Entity Framework context.sleep.Where(o=>o.clientDateTime.TimeOfDay>new TimeSpan(18,0,0)).ToList(); Please suggest me a workaround.
Is there a simple alternative in PostgreSQL to this statement produced in Oracle? select
I'm practicing Jquery and I've written this simple Jquery statement: var someText = $(table
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =
This would be easy to do with regular array with a simple for statement.
I have the a simple LinqToSQL statement that is not working. Something Like this:
This statement is returning error 00920- invalid relational operator. I am sure it is
this statement will generate a 4 column table: SELECT shipped.badguy AS badguy, shipped.sdate AS
In this statement, taken from the Pagerank source code: Pattern.compile(\\[.+?\\]); What does the pattern
If I use this statement: $query = select * from Products where category_description='.$search_dept.' ORDER

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.