Variable won't evaluate correctly in Javascript

Joined
Jan 29, 2023
Messages
1
Reaction score
0
I'm developing a javascript program that will take info from a user prompt and use that info in an if statement.
the output from the script is stored in a variable NEW_LINE which will be used in an if statement.
in one example, NEW_LINE generates this value: (input_line.indexOf("bob") != -1) || (input_line.indexOf("robert") != -1)
The following statement always evaluates as true even when it is false.
if (new_line)
{alert("new_line = true");}
else {alert("new_line = false");}
However, when I copy the contents of NEW_LINE into the if statement as shown below it always works correctly.
if ((input_line.indexOf("bob") != -1) || (input_line.indexOf("robert") != -1))
{alert("new_line = true");}
else {alert("new_line = false");}
Anyone have any ideas? Why do they evaluate differently as they are evaluating the same content?
It's driving this 81 year old crazy.
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
if ( new_line ) return true because the var exists. it's a weak checking ,faulty condition.

if ( ....indexof || ...indexof ) return true because the two strings are not found ( or just one of both ) .
remenber indexOf('scheme') return -1 when the string 'scheme' is not found ... this test is useless

to use and check the prompt() dialog , read about :
JavaScript:
https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
The difference is because of the type of the value stored in NEW_LINE. In the first case, NEW_LINE is a string, not a boolean or an expression that returns a boolean.

In JavaScript, any string except for an empty string is considered to be truthy.

When you use new_line in an if statement, it evaluates as true because it's a non-empty string. To get the desired behavior, you need to evaluate NEW_LINE as an expression, not just as a string.

One solution is to use eval() function, like this:

Code:
if (eval(NEW_LINE))
{alert("new_line = true");}
else {alert("new_line = false");}

However, be cautious when using eval() as it can have security implications if the input is not properly validated. An alternative solution is to assign the value of NEW_LINE to a new variable that can be evaluated as an expression.

Code:
let line = (input_line.indexOf("bob") != -1) || (input_line.indexOf("robert") != -1);
if (line)
{alert("new_line = true");}
else {alert("new_line = false");}
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
JavaScript:
const names = ['bob', 'robert'],
         name = prompt('What is yor name?') ?? 'Rocky';
   alert( 'new_line = ' + names.some( x => x == name.toLowerCase() ) );
 
Joined
Jul 12, 2020
Messages
89
Reaction score
9
The following statement always evaluates as true even when it is false.
if (new_line)
{alert("new_line = true");}
else {alert("new_line = false");}
In the above code the condition returns "true" only if the variable exists, which I'm assuming it's set using the prompt. To test, you simply change the name of the variable at the prompt, leaving this portion of code untouched, and run it again and see what happens. It should return "false" because the prompt doesn't create and set the variable
NEW_LINE generates this value: (input_line.indexOf("bob") != -1) || (input_line.indexOf("robert") != -1)
Code:
var new_line = (input_line.indexOf("bob") != -1) || (input_line.indexOf("robert") != -1)
The value of this "new_line" variable should be either "empty" or "null"
However, when I copy the contents of NEW_LINE into the if statement as shown below it always works correctly.
if ((input_line.indexOf("bob") != -1) || (input_line.indexOf("robert") != -1))
{alert("new_line = true");}
else {alert("new_line = false");}
Anyone have any ideas? Why do they evaluate differently as they are evaluating the same content?
In the above code here, it's not checking the existence of the "new_line" variable as before, it's checking the "input_line" to see if it contains the name "bob" or "robert". If it contains one or the other in any form it will return "true", meaning even if the name posted is "plumbob" or "roberto". It will still return "true" simply because "bob" or "robert" is located in the string. The indexOf method locates the starting postion of the text you're looking for and returns it as a number. This number can be anything from "0" or higher within the string being searched. That is why the test states "!= -1" (not equal to negative one) negative one means the text does not exist in the string, anything else means it does (in any form)

Another thing about variable and function names most people don't know is that they can collide with programming variables created for the application or operating system. If this happens your script will throw an error even if it should work correctly. The OS or application variables and functions handling the script will always take precedence.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top