-4
<html>
<head>
<title>Beast Mode ON</title>

<style>

</style>
</head>
<body>

<script type = "text/javascript">
var name = prompt("Please enter your name: ");
if (name == null || name == "") name = " visitor "
{document.write("Hi " + name + " welcome to javascript.");}
 </script>

</body>
</html>

this code works accurately. the question i wanna raise is WHY? because the "if" statement executes the codes in curly braces only if the condition in the paretheses is met. but if i try to incorporate parentheses like

if ((name == null || name == "") name = " visitor ")

the code stops working. what's the reason?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Rajat Chauhan
  • 13
  • 1
  • 6
  • 1
    The code in the curly braces [always runs.](https://jsfiddle.net/u002zhng/) What do you think `if((name == null | name == "") name = " visitor ")` should do? Basically, it's like writing `if ((true)name = "visitor")`. It's syntactically invalid, doesn't denote a block, and implies that you can do things like `(true)name` and expect a result. – Mike Cluck Oct 21 '16 at 20:39

4 Answers4

1

The general structure of if is:

if (condition) thingToDo

When the condition expression is true, it executes the code in thingToDo. (I've deliberately left out the optional else clause, as it's not relevant to this question.)

So in your first block of code, condition is name == null || name == "" and thingToDo is name = " visitor ". So if the user enters an empty name in response to the prompt, it sets name to " visitor " in its place.

If thingToDo contains multiple statements, you have to wrap them in curly braces to make it a block of code. But if it's just a single statement, the braces are optional (although I recommend them, see Why is it considered a bad practice to omit curly braces?). The braces around the document.write() call are irrelevant, as that is outside the if statement.

Your second attempt is wrong because you've change the condition to:

(name == null || name == "") name = " visitor "

This is not a valid expression -- you've moved the assignment inside the condition, but there's no operator connecting it with the comparisons.

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Its because, it is incorrect syntax

if ((name == null || name == "") name = " visitor ")"

if is not being closed correctly

It should be something like this

if((name==null||name=="")) name="visitor";

Hope this helps

Barmar
  • 741,623
  • 53
  • 500
  • 612
Geeky
  • 7,420
  • 2
  • 24
  • 50
0

I have 3 suggestions

  1. the correct code would be

<script type = "text/javascript">
    var name = prompt("Please enter your name: ");
    if (name == null || name == "") 
    {
        name = 'visitor';
    }
    document.write("Hi " + name + " welcome to javascript.");
</script>
  1. please use single quote in javascript instead of double quote.
  2. its better to use a library to check for all false cases of a variable. Since it could also be undefined. This also makes it easier to read. You could use the library lodash and write if(_.isString(name))
  • 3
    Why use single quotes instead of double quotes? That's just personal style, there's no reason to prefer one. – Barmar Oct 21 '16 at 20:43
  • You've broken the functionality though. The idea of the `if` is to see if they should change `name` to `" visitor "` when the user didn't enter anything. Whether they entered anything or not, it should be writing out the message. – Mike Cluck Oct 21 '16 at 20:45
  • thanks... i got this. but my curiosity was regarding the visitor part.. how there came to be a condition out of the parentheses ? – Rajat Chauhan Oct 21 '16 at 20:49
  • well if you are goin to mix javascript and html its easier using different kind of quotes. – CG Christen Christensen Oct 21 '16 at 21:07
  • @CGChristenChristensen That's true, but they both allow either kind of quotes, so you can use double in JS and single in HTML, or vice versa. – Barmar Oct 21 '16 at 21:08
  • @MikeC so I just changed the code, I guess this would make the most sense then. – CG Christen Christensen Oct 21 '16 at 21:11
  • @Barmar you can do many things, but if you want other people to read your code, you should use double quotes in html and single qoutes in javascript. That is what most people expect. – CG Christen Christensen Oct 21 '16 at 21:12
  • 1
    I don't agree with that statement at all and I've been working in the industry for years. – takendarkk Oct 21 '16 at 21:19
  • That's news to me. I've been here for years and never noticed a concensus one way or the other. – Barmar Oct 21 '16 at 21:19
  • @CGChristenChristensen I prefer single quotes because that means I don't have to press Shift when I'm typing them but they're by no means an industry standard. Everyone has their own preference and, truth be told, your HTML and JavaScript should seldom, if ever, meet so that kind of throws your argument out. – Mike Cluck Oct 21 '16 at 21:20
  • I agree that HTML and JS shouldn't meet :) but regarding the quotes I guess we have different opinions. – CG Christen Christensen Oct 21 '16 at 21:33
  • http://stackoverflow.com/a/31883471/1814303 – CG Christen Christensen Oct 21 '16 at 22:03
  • guys thanks a lot for such an overwhelming response. acutally i was trying this snnipet from a book and wondering how an if statement could be extended out of the parenthesis. but as explained by @CG Christen the name = "visitor" was actually and assignement, not the conditional. Anyways, you completely nailed it. now it has been cemented in the deepest part of my neurons. – Rajat Chauhan Oct 25 '16 at 06:45
0
if (name == null || name == "") name = " visitor "
{document.write("Hi " + name + " welcome to javascript.");}

This is incorrect syntax.

What it is saying (and doing) is this:

if (name == null || name == "") {
      name = "visitor";
   }
document.write("Hi " + name + " welcome to javascript.");

name = "visitor" is not a condition. It is an assignment.

In your code, the braces around the document.write... are useless and do nothing for the if statement.

You don't have to use braces around a one line if statement, but the correct way to write this would be:

if (name == null || name == "") 
    name = "visitor";
document.write("Hi " + name + " welcome to javascript.");

The above will only work if there is only one line to the code executed after the condition. Any other code is executed outside of the if.

user1289451
  • 911
  • 8
  • 22