This is a complete js script I wrote while teaching myself js:

/* The Extremely Long Compliance Quiz (TELCOQ) copyright (c) 2013 by Miki Kocic. All
 * rights reserved except that his program is being released under the GNU Public
 *  Licence version 2 or later, with the version later than 2 being at your
 *  discretion. */


 // declare variables

 var introString = "Welcome to The Extremely Long Compliance Quiz! This \
 excruciatingly endless quiz tests whether you can follow instructions.";

 var userPrompt = "Type in 'yes' to take the quiz or 'no' to skip taking it \
 because you have better ways to waste your time.";

 var outcomeOne = "By typing either 'yes' or 'no' rather than something else, you \
 showed that you can follow instructions. You pass the quiz! Congratulations!";

 var outcomeTwo = "I'm sorry that you don't want to take my quiz, but by typing \
 either 'yes' or 'no' instead of something else, you took it whether you wanted \
 to or not! How's that for infringing on your civil liberties? The good news is \
 that you passed the quiz by showing that you know how to follow instructions.";

 var outcomeThree = "By typing something other than 'yes' or 'no' as instructed, you \
 took the quiz and flunked it by showing that you don't know how to follow \
 instructions. Sawry!";

 var UserChoice

 var outputString

 var closingMessage = "Thank you for using The Extremely Long Compliance Quiz. \
 Have a nice day even if it's past midnight!";


 // print intro message and request user input

 alert(introString);
 userChoice = prompt(userPrompt);


 // main code

 switch (userChoice)    {
     case "yes":
        outputString = outcomeOne;
        break;
    case "no":
        outputString = outcomeTwo;
        break;
    default:
        outputString = outcomeThree;
        break;
}

 // print output and closing message and exit

alert(outputString);
alert(closingMessage);

Have fun with this thingamajig. I sure enjoyed writing it!

Miki

Line 27, you declared a variable with upper case, but then later on you use it with lower case. It means that the variable created in that line has no use. Better declare & initiate it at the same time at line 38 var userChoice = ....

Thanks for pointing out the typo, Taywin. You raise an issue I've wanted to discuss with a more experienced programmer for some time. The approach I'm trying to get into the habit of using is this one:

// declare variable

var firstVariable

// use variable

...some code that assigns a value to variable...

But you appear to be suggesting this approach:

// declare and use variable at the same time

...some code... var firstVariable ...more code that assigns a value to the variable...

Please let me know why you believe that the latter approach is superior. I'm willing to listen and think about what you've said.

Progz

It is simply a style of programming. You could do either way. Though, each technique has its advantages and disadvantages.

Declaring all variables used in your program/script at the top, and then use them again below is similar to old C-language programming style. The style is not necessary to be good any more because of variable scope concept (except including modules or libraries).

It is a good idea to see all variables declared at the beginning of the program because you may forget where it is being declared later on.

Also, declaring a variable in the outter most would allow the variable to be global to all others inside the same scope. This purpose is very important in many different cases in programming. You may need to read more on variable scope.

One downside of declaring on the top is that you may have a typo in your program. This downside would be fixed at compile time in a compilable language. It would be caught at run time in a functional/scripting languague.

Another downside is that a compiler will need to keep the variable alive when the variable does not really need to be alive until it is being used. As a result, the program require more space (memory and/or storage) while it is running. However, this downside is fixed by compiler optimization in a compilable language. I am not sure whether functional/scripting languages could do the same (and I think they don't).

When declare a variable right at where it is being used would ensure that the variable name being used is correct. The downside is that the variable will be seen only in the scope where it is being declared (refer to variable scope again).

Usually, any variable I declare down the line will be used as local variable and I would not use it through out the program/script. In other words, I would use it and discard (not use) it again later on as if it is a temporary variable.

Thus, you could do it either way. The only thing that you need to keep in mind is the scope of the variable you are using. In JavaScript, it is a bit different from other languages. Javascript allows you to assign a value to any variable without declaring the variable (using var). As a result, the variable being assigned value to becomes global (can be seen anywhere).

var a = 1;
alert(a);  // displays '1'
alert(b);  // error because b is undefined
function something() {
  b = 3;
}
alert(b);  // now it is OK and displays '3'

Remember that variable declaration is important and should be declared regardless where it is being declared. It is all about how you use it.

Thank you for providing the extensive information you provided. I do indeed intend to read up on variable scope once I have enough general programming knowledge to understand what's being said. (I only recently learned what "method" means after having struggled through some more advanced material that discussed various methods.)

One thing I am curious about is that, in your code sample, you use "b=3" within the function rather than "var b=3" and then use the variable outside the function even though it hasn't been externally declared. I've been taught that a variable declared within a function has only local scope and cannot be used outside the function without returning an error. Is that not correct? And if it's not necessary to declare the variable using "var," then why ever use "var?" Please let me know.

The example is just to show you how JavaScript works with variables. If you do not declare it inside a scope, it will automatically become global.

alert(b); // error because b is undefined
function something() {
  b = 3;
}
alert(b); // now it is OK and displays '3'

Now, if you do it this way...

alert(b); // error because b is undefined
function something() {
  var b = 3;
}
alert(b); // error because b is undefined (out of scope)

Thanks very much for letting me know that. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.