To var or not to var?
This is more of a curiosity thing than anything, but while working through these lessons of declaring string variables the instructions say to use:
var myVariable = "string"
but I noticed that no errors pop up if i simply write:
myVariable = "string"
Now, is there any purpose to writing var before I declare my variable? Is it necessary and the Codecademy compiler can read it with or without it, or is it more of a coding etiquette thing. Thanks for your help.
Answer 513952741534c9b12e0000fc
Aaaah I see….interesting question. Well it’s a matter of the scope of the variable. Say you have defined a variable at the beginning of your program:
var i = 5;
And then inside a function you write:
i = 0;
Then you are changing the value of the i variable you declared at the beginning of your program.
But if inside this function you wrote:
var i = 0;
This is a new variable called i that you can only use inside this function (of course this means you will not be able to use the other variable you defined at the beginning of the program, because they are called the same). These variables you define inside functions are called local variables, which means they are local to the function, but as soon as you come out of the function they are eliminated. If you declare a variable at the beginning of your program, it is called a “global” variable that you can use anywhere.
So basically declaring a variable or not just depends on what you need. Do you need to operate with the global variable? Or will you use a temporary, local variable? Will you use the same name for both variables? Then you have to deal with the problem of whether you declare a new variable or not.
Did you understand? If not, let me know and I will try to explain it better.
Answer 5139630ff254e413e300090e
Actually, I had never thought of that, and although it seems like poor coding to use the same variable name to represent two different numbers, just because it seems like there could be some major issues with debugging, I will keep that in mind simply as something to remember. I’m guessing that also means that if I declare a variable inside of a function, then I won’t be able to use that particular variable or version of that variable outside of the function if I think to call it inside another function? That would be interesting to know. But anyways, back to my original thought, because that isn’t exactly what I was trying to get at, maybe I worded it wrong.
What I was really referring to was that while playing in the javascript labs i found out that on a blank slate, you can simply type:
myName = "Josh"
instead of:
var myName = "Josh"
when you are declaring a variable for the first time and the IDE still pops up with an alert if, say, I type:
if(myName.length === 4)
{
alert("True");
}
What I am trying to figure out is if using var when declaring a variable for the first time. I am slightly familiar with Java and I understand that for an interger or a boolean to be declared for the first time, you MUST type int or bool before typing either one respectively. I feel like it is important to type var when declaring a variable in JavaScript for the first time, and that this is just a minor glitch in the program used on Codecademy, but I wanted to check for sure. I cant really find much on it online.
Answer 51397205a6c49348da000316
Oh, don’t worry, it’s not a glitch in the codeacademy program. This is a particularity of the Javascript language, and many of us don’t know why it has not been addressed yet. It really is bad practice to use a variable without declaring it first. There are many reasons, and it may not necessarily be poor coding on your part. For example:
Consider a huge program which is being edited by many people. Someone declares a global variable with the same name as your local variable, then you get an error and you might not know what happened, because the compiler won’t tell you.
Imagine you have several nested functions, local variables in those functions can hide local variables from parent functions as well. That’s another problem to consider.
These kinds of errors are actually pretty frequent and usually happen when people use indexes for loops without declaring them, and they don’t realize they might have accidentally bounded them to variables in parent functions or global variables.
Also, these kinds of errors are very difficult to find since the compiler will not give you an error, you will just get the wrong result. So you will have to waste a lot of time printing values on and on until you notice the anomaly.
It is also a bad practice because when you start programming with statically typed programming languages (Java, C++, C, etc), then you will need to declare your variables with proper syntax, and it will a pain to get used to doing that instead of what you used to do before.
Scripting languages are good for learning fast, but they can create bad habits that will translate into difficulties when you work with programming languages.
Answer 513986b3ce829a0cfa000a1a
Ah hah. That is exactly what I was looking for, and honestly the only reason I even realized the difference instead of picking up the habit of not typing
var variable = value;
and just:
variable = value;
is because through the little experience I have gained teaching myself Java. I don’t have any schooling beyond high school, and my high school never offered any classes involving computers beyond Microsoft Word. Ha. So your last post inspired me to do some research. I get that if I have a nested function and I want a variable within that function to to be hidden from the parent function, then I must declare that variable as static, but what exactly do you mean when you say Java is statically typed?
I found that a scripting language is built to run commands that could be manually clicked or ran by a user in a specific program (for example finding the number of times someone tweets about Charlie Sheen) but would be tiresome and tedious for any person to do, but I couldn’t find any references to “static languages.”
Now, can you still declare a variable as static in JavaScript? And I’m sorry for asking so many questions, haha, you are just someone who obviously has had much more exposure than I have, and every time you answer, you answer with enough detail to cover my curiosity and expound on a whole new topic I haven’t touched.
I have purchased a few books lately, Java Programming and Programming Logic and Design by Joyce Farell, both of which I am starting on Monday, and I have a few books on HTML, CSS, and JavaScript, so I’m sure I’ll find most of what I want to know eventually, but I really enjoy this stuff. What I am getting at, is if I am bothering you with so many questions, do you have any good references for a beginning programmer. Maybe a good programming forum, or someplace like that to just dive my head and indulge?
Answer 5139ba524d90ee65e70019ea
Aha! Very good question. Most programming language compilers (programs that translate what you write into binary code that can be executed by the computer, basically, a “translator”), have something called type checking.
Type checking is something compilers do to make sure your variable declarations make sense and/or have been written properly.
Programming languages, such as Java use static typing, this means they do type checking in compile time (when the compiler is translating your code). So if you are programming in Java, and you make a mistake in your variable declaration, for example:
int x = “Hello”;
Then you will get an error, and the compiler will not compile your program. So your program was not even translated(compiled) into binary, an even less was it executed.
Scripting programming languages, such as Javascript use dynamic typing, this means they do type checking in run time (when the program is being executed). This allows for some flexibility that isn’t always convenient for you. In the case of Javascript, when the program is being executed the computer thinks: “Oh, this variable hasn’t been declared yet. Well, let’s just pretend it was declared and use it anyways”.
There is another notable difference between scripting programming languages such as Javascript, Python, Ruby and programming languages such as Java, C, C++, etc. These scripting languages are not “compiled” but rather “interpreted”. The only difference between compiled and interpreted programming languages is the following:
When you compile a programming language, you translate all of it, you produce an output (an executable file) and then you run this program. Whereas interpreted programming languages translated and executed right away. For example, if you download the python interpreter http://www.python.org/getit/ (version 2.7.3 recommended) and you execute it, then you will enter a console-like environment where you can execute things such as “3+4” and you’ll get as a result “7”. You can also execute “scripts” if you make a .py file, you can run it like this python yourscript.py or if you are in windows just double click on it like an executable. You cant do this with Java. You need to write a .java file, and then you need to compile it with the javac compiler and then you need to execute the output.
Declaring a variable as static in Javascript…yes you can do that. To understand it clearly you would need some background in object oriented programming and how it weirdly translates into Javascript. But I’ll try to make it simple. Here’s an example:
function money(){
if (typeof money.counter === 'undefined'){
money.counter = 0;
}
money.counter++;
}
The variable counter is static as it is a “property” of the function. Hope that makes sense. If not, don’t worry, you’ll get it sooner or later. Just remember you can create classes with functions in Javascript. In javascript, I prefer using global variables instead. It’s less confusing.
Please, ask as many questions as you want. I will joyfully answer all of them whenever time allows me to.
And yes I have some good resources for you. First finish at least one (ideally all) the courses in codeacademy. Then go here: https://www.udacity.com/course/cs101 Then afterwards you can read books like these: http://www.goodreads.com/list/show/12811.Top_Ten_Most_Influential_Programming_Books_of_All_Times
What’s most important is learning how to program. When you can program, doing it in different languages is just a matter of syntax, just a matter of how to express things, but you will already know how to do it. So don’t focus yet on a programming language in specific, focus on the methods, the algorithms, the operators, etc.
Some additional resources to get you inspired: http://www.catb.org/esr/faqs/hacker-howto.html http://samizdat.mines.edu/howto/HowToBeAProgrammer.html
Don’t worry about not having a degree in computer science. I’ve known PhDs and Masters in Computer Science who cannot write even a simple program. That’s why all computer programming interviews ask you to write some code to demonstrate that you know what you are doing.
The degree certainly helps for the salary and makes it easier to have a programming job. But, street cred is everything. Work on an open source project, contribute code, make friends, and you’ll most surely get a job doing what you love. Who knows, you might even end up at Google.
You can contact me at my email if you ever have any other questions: [email protected]
Happy hacking!
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons