Syntax, particularly with using variables


#1

This is a really simple example because I'm just starting to finally learn programming, but I have some general questions.

var orangeCost = function(cost) {
    console.log(cost * 5);
}
x = prompt("How much do the oranges cost this week?")
orangeCost(x)

It seems like I can just use the variable x without putting var in front of it. I feel like I shouldn't be able to do this, but I also think maybe it's enough that x is defined. I also don't know whether the workspace has some weird things built in, such as allowing defined letters/words be used in functions, nor whether that's a reasonable thing to be worrying about. I don't know what types of situations I might end up using JS, and I don't know whether I will need to worry about any kind of formatting differences across workspaces(for lack of a better term).
I'm also not clear on why I don't need a semicolon after the prompt and after orangeCost(x).

These feel like grammar questions. I would also appreciate any extra information regarding syntax that I'll end up learning in the future, just because I would like to be thinking about all of that throughout learning how to use the language.


#2

In the present learning environment we spend most of the time in global window context, so we can learn and familiarize ourselves with variables and constructs. However we soon learn that our variables really don't belong in that context. They belong in function context with their own local scope.

That's where var comes in. Now that we've got into the habit of using it, we can continue to do it with purpose. Variables are always in the scope of their declaration.

var myVar;
function myFunc(myVar) {
    return "local: " + myVar;
}

console.log(myVar); // undefined
console.log(myFunc(myVar)); // local: undefined

Now let's define the local variable,

function myFunc(myVar) {
    myVar = "locally defined";
    return "local: " + myVar;
}

console.log(myVar); // undefined
console.log(myFunc(myVar)); // local: locally defined

It's well within the realm of JavaScript, and touted as the optimum approach to never use globally scoped variables. Protect the global window context from our variables, is generally how it is spoken. Sage advice that learning more about the importance of var will reveal and verify.


#3

Thank you for the explanation, but unfortunately I don't think you addressed my core questions.

I don't understand why I was able to use the variable x without declaring it a variable first.

I also am not exactly sure when I need a semicolon and when I don't, particularly in the example I provided (which is simple enough for me to currently understand).

Again, I don't have any background in programming so parts of your response went over my head. I don't yet know what the global window context is, and I don't know how to structure code in my head yet - I'm not sure what is or can be global/local. Where is a variable x if it is NOT declared (as in my example) and how do I change its scope? Or is that something beyond my current level of understanding? (I am just now beginning the section regarding global and local variables, but would of course appreciate any further explanation you or anyone else is willing to give).


#4

For a good guidance
use www.crockford.com
for instance
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
or
= http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org
javascript variable site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals
javascript return statement site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

== discussions / opinions ==
javascript [your question] site:stackoverflow.com
javascript return statement site:stackoverflow.com
= http://stackoverflow.com/questions/3717420/is-there-a-benefit-to-using-a-return-statement-that-returns-nothing
= http://stackoverflow.com/questions/22134753/return-statement-in-javascript

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html


#5

Variables can be defined without being declared.

x = 5;

However, regardless where it is defined, in a function or not, it is hoisted to the top of global scope.

function foo(){
    x *= x;
    y = 1 / x;
}
console.log(x); // 25
console.log(y); // 0.04

We defined x in global scope, and can access it from inside the function. We defined y in local scope, but since it is not declared, it is hoisted to the top of global scope, alongside x. This is why we can access both variables from outside of the function.

You will learn that this is not a good practice, but for now in the safe environs of the course IDE we can forego best practices and try it out for as long as it takes to learn it.

Intuitively we both agree. The practice of not declaring variables should not be allowed but JS allows it. Chalk it up to the nature of the language and set your sights on best practices, namely that of declaring variables in the scope where they are needed, and not above. The less we declare in global scope the better it is for all..


#6

I'm sorry I didn't see these responses sooner - for some reason I didn't get notifications after my first reply.

Thank you both for your additional input, I'll definitely be using the sites/pages @leonhard_wettengmx_n suggested and @mtf, that really clears up a lot of stuff I was thinking about, thanks again.

Ultimately I probably just need to get through the simpler exercises until I am comfortable enough with the language to understand more nuanced things.