Right or wrong

Joined
Nov 14, 2024
Messages
2
Reaction score
0
Hey everyone,

I have a question. I started learning JS and i have been a task to solve. The task is Not the problem. My solution checks for strict equality between indexOf and 0 in the if condition. While the sample solution checks for inequality on strike. So indexOf and -1. My question is, is it more correct to check for inequality (so -1) or equality (so 0)?

If (variable.indexOf(variable) === 0)

Or

If(variable.indexOf(variable) !== -1)
 
Joined
Sep 4, 2022
Messages
136
Reaction score
16
Hi Tobi1987 !

the difference belong to your choice, it's only about what you prefer.

your need is accurate , let rewrite those constraints :

you want to test if the 'element' is at '0' index in the main string.
the better the more readable is for convenience :

If (variable.indexOf(variable) === 0)
// because you are on the more pointful writing.

by going on checking "-1" as return value, you are on a wider testing,
passing the test like that will let all others index positions for the 'search string'.
ex : if the search string is located at index 8 ... the "-1" awaited will fails. same result if the search string is at 0 index ( your need to cover.

by JS, you can be more tricky, JS is DOM tool, It's 'string based' and that open some easy going practices, see this one :

- you can crawl through an 'integer' , the same way you can go through a 'string'

JavaScript:
// three vars : 1 integer , 1 string , 1 array

var the_integer = 10456 ;
var the_string = "atoms" ;
var the_array = [ "sun" , "rain" , "snow" , "fog" , "cloud" ];


// by JS features, and index uses :

console.log( the_integer[2] ) ; // print '4'
console.log( the_string[2] ) ; // print 'o'
console.log( the_array[2] ) ; // print 'snow'


// and for your need, using this JS feature :

// -- scope start

var variable = 'abcdefgh' ;
var search = 'a' ;

if( variable[0] === search ){ // you're at it. by testing the first slot of 'variable' === 'a'

 ... ;

 }
// scope end --
 
Last edited:
Joined
Jul 4, 2023
Messages
503
Reaction score
63
The choice between indexOf(...) !== -1 and indexOf(...) === 0 depends on what exactly you want to check:
  • indexOf(...) !== -1: Using this condition, you're checking if the element exists anywhere in the string or array.
    • -1 - means not found - false
    • 0, 1, ... etc. - means found - true
  • indexOf(...) === 0: This checks if the element is specifically at the beginning (position 0 - at the start of the string or array).
    • -1, 1, 2, ... etc. - means not at position 0 - false
    • 0 - means found at position 0 - true
BTW, If you simply want to check that the element appears somewhere in the string or array, use indexOf(...) !== -1 — this is also equivalent to includes, which returns true or false based on the element’s presence.
JavaScript:
const text = 'Hello, world!';
if (text.indexOf('world') !== -1) {
  console.log('The element is present - text.indexOf', text.indexOf('world'));
}
if (text.includes('world')) {
  console.log('The element is present - text.includes', text.indexOf('world'));
}

const array = ['Hello', 'world', '!'];
if (array.indexOf('world') !== -1) {
  console.log('The element is present - array.indexOf', array.indexOf('world'));
}
if (array.includes('world')) {
  console.log('The element is present - array.includes', array.indexOf('world'));
}

JavaScript:
const text = 'Hello, world!';
if (text.indexOf('Hello') === 0) {
  console.log('The element is present at the start - text.indexOf', text.indexOf('Hello'));
}
if (text.includes('Hello')) {
  console.log(`The element is present at the ${text.indexOf('Hello')} position - text.includes`);
}

const array = ['Hello', 'world', '!'];
if (array.indexOf('Hello') === 0) {
  console.log('The element is present at the start - array.indexOf', array.indexOf('Hello'));
}
if (array.includes('Hello')) {
  console.log(`The element is present at the ${array.indexOf('Hello')} position - array.includes`);
}
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,031
Latest member
AndreBucki

Latest Threads

Top