0

I have an if-else statement in JavaScript which returns false, but doesn't execute the code inside 'else'.

part of the script inside the cshtml file:

if (@Session["mission"].ToString() != "1") {
    setInterval(function () { myTimer(ctxPoints, ctxLines); }, 1000 / @ViewBag.rate);  
} else {
    alert("hi");
}

In the debug the '@Session["mission"].ToString() != "1"' statement returns false, but nothing jumps to the screen.

10
  • What is @Session["mission"].ToString()? Did you mean toString? Commented May 30, 2019 at 9:06
  • 1
    please show the result of @Session["mission"].ToString(). Commented May 30, 2019 at 9:11
  • seems like Razor C# syntax so it is C# .ToString() with uppercase, but you should check the value of @Session["mission"].ToString() to make sure it is what you expect Commented May 30, 2019 at 9:11
  • you might need to do a null checking on @Session["mission"] before the if else statement, just to ensure that it is not null Commented May 30, 2019 at 9:11
  • It is not null.. the '@Session["mission"].ToString()' returns "2" (string type) and the '@Session["mission"].ToString() == "1"' returns false (bool type) Commented May 30, 2019 at 9:14

2 Answers 2

3

You should open your web browser Developer console and see the real script which reaches the browser.

The Razor parts, @Session["mission"].ToString() doesn't exists in the browser script, because they are evaluated in the server, before sending them to the browser. So it makes no sense to try @Session["mission"].ToString() != "1" this in the console. In the browser you will get something like: if (1 != "1") or even like if ( != "1") which would provoke an error.

JavaScript does coalescing, so 1 is equal to "1" when you use the comparers == or !=, so you don't need to include the qoutes around the value (unless you get the error mentioned above: in this case, if you included the quotes, you'd get if ("" != "1")).

Coalescing is avoided by using the !== or === which would consider 1 not equal to "1", so, including the quotes doesn't make any difference in this case.

Sign up to request clarification or add additional context in comments.

Comments

2

I think you should clear Js code and c# code

if ("@Session["mission"].ToString()" != "1")

Session["mission"].ToString() this print string in c#, but you need to encapsulate it in quote to make this value get type string

4 Comments

hmm I can't see how that can be an issue even if it is if (1 != "1") instead of if ("1" != "1"), and I am guessing the quotes inside might need to be escaped
@slai you are right, my answer is only helping him has safer code. Problem must relate to another code problem
Downvoted beacuse the first comment is right. if you want to explain how to write "safer code" you should mention the difference between != vs !== and == vs ===
@JotaBe Yes, I upvoted for your answer because It is detail enough

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.