Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
znzlcrstkk
Front End Web Development Techdegree Graduate 28,511 Pointsvariable and function
Hello can somebody explain to me about variable in javascript, for example: var guess, and it has no value, so what can i do with such type of variable?
And the second question is about javascript function, i dont understand one part, for example function (star, sky) {} what position takes "star" and "sky" in this function, what they do?
And please give me some examples. Thank you.
4 Answers
Alexander La Bianca
15,960 Pointsvar guess; will just initialize a variable. But it's value will be undefined. For example:
var guess;
console.log(guess) //this will log out undefined
//where
var anotherGuess= "A guess"
console.log(anotherGuess) //this will log out 'A guess'
function logGuess(parameter) {
console.log(parameter);
}
logGuess(anotherGuess) //will log out 'A guess'
whatever is within parenthesis in a function is a parameter and can be used within the curly braces of the function.
Steven Parker
243,266 PointsYou're right, that bit of code that declares the "guess" variable doesn't give it a value, but now it can be given a value in an assignment statement, perhaps like "guess = prompt('What is your guess?');". An example of where you might want to declare a variable separately from the assignment would be if the assignment was done inside a loop.
In your example function, "star" and "sky" are both parameters which represent values that will be supplied when the function is called at a later time.
znzlcrstkk
Front End Web Development Techdegree Graduate 28,511 PointsAlexander, but in some videos i saw, when at the begining of the code variable has no value, and then later at the middle of the code the same variable has the value, so whats happening?
znzlcrstkk
Front End Web Development Techdegree Graduate 28,511 PointsSteven parker, can you give me an example with parameters that will be supplied, when the function is called?
Steven Parker
243,266 PointsYour snippet doesn't show what the function name might be, but let's say it was "twinkle" ("var twinkle = function (star, sky) {};"), I'm also going to guess that the parameters are intended to be strings representing colors. So in that case, calling the function might look like this:
twinkle("yellow", "blue");
So while the function runs, the variable star will have the value "yellow", and sky will have "blue".