While keyword question from a newbie

Hello,

i am new to JS

been working on the while keyword

 var balls = 5;

while (balls > 0); {

document.write ("another ball!<br>");

balls = balls - 1;
document. write ("juggling is a hobby");

}
            

it wont show in my web browser, not sure why

interesting i successfully achieved that with other code

i know very little JS, so this is foreign and i am on a learning curve at this point

help, please : )

Look veerrrry closely. There’s something there that doesn’t belong.

2 Likes

Also, I’d be surprised if these lines works (hint: spaces)

document.write ("another ball!<br>");
document. write ("juggling is a hobby");

Prepare to be surprised then.

Javascript doesnt much care about whitespace. It’ll happily process

document.write("thing");
document .write("thing");
document . write ("thing");
document
   .write
  ("thing");

basically it’ll only start complaining when your tokens stop being tokens. Or more specifically, when it interprets your tokens as multiple other tokens:

document.wr ite("thing")

is going to cause a problem because “ite” is an unexpected identifier at that point.

1 Like

thank you for your time and guidance!

i have been adjusting my code in my text editor… cannot make it work

kindly instruct where my problem is

this will ultimately help my JS skills

thanks again!

a ; is the end of a line in Javascript.

reads as while (balls > 0); end of sentence.

This is still valid syntax. It’s an infinite loop, but it’s valid syntax. It says “While the value of balls is greater than 0, do nothing, and go back to the beginning of this sentence.”

Remove the semicolon.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.