FAQ: Variables - Create a Variable: var

This community-built FAQ covers the “Create a Variable: var” exercise from the lesson "Variables ".

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Create a Variable: var

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: Do we need apostraphes for number variables?

Hi there,

Not sure what is going on…
After creating the vars below, it is asking to print the values on the console…
var favoriteFood = ‘pizza’;
var numOfSlices = 8;

console.Log (favoriteFood);
console.Log (numOfSlices);

we did the above, but it keeps telling me it is wrong…

What did I miss? Thx

UPDATE:

I got it!!!

console.log it is NOT in camel form! For that reason, console.log shouldn’t have the second word started with capital as I wrote initially… now that bamboozled my head a bit…

1 Like

2 posts were split to a new topic: When to use var or let?

Hello, I have som questions.

If I type var numOfSlices = number(8) the checkmark on the left with the instructions takes it as a correct answer but on the right side the “terminal” says there’s a syntax error.

I saw the hint and it says to declare the variable using this way var newNum but how do I assign the name of the variable using this?

I’ve seen that most of the answers are var numOfSlices = 8

So I’m confused, why does the checkmark takes it as a good answer if there is a syntax error, when do I use the var newNum and how do I name it.

Thank you very Much!

All assignments use the = (assignment operator).

let variable = value

The value may be any data type or expression.

let numberOfSlices = 8

assigns the value 8 to the variable, numberOfSlices.

We wouldn’t normally use the Number constructor on number literals. Instead it is usually applied when it is given a string value or some expression that can be coerced to a number literal.

a = '8'
b = Number(a)
console.log(b)    // 8
3 Likes

Thank you very much! This was very helpful!!

1 Like

Got stuck for about 5 mins on this until I realised that ‘favourite’ is spelled ‘favorite’ :sweat_smile:

British vs American grammar.

British vs. American spelling. Both languages have the same grammar, but differ with respect to u being removed in American spelling.

var favoriteFood  = 'Pizza';
var numOfSlices = 8;
console.log(favoriteFood);
console.log(numOfSlices);

Everything seems to be right but can not pass the exercise. Where is the mistake ?

strings (like pizza) should be an exact match, this include uppercase/lowercase letters

Seems to work now, thanks

2 posts were split to a new topic: [REPORTED] Incorrrect error message in JavaScript var lesson

This description on variables in the link below really helped me understand the bigger picture, It’s particularly useful if you are a “visual” learner. Thought I’d share. Cheers!

[Variable Analogy]

(https://blog.codeanalogies.com/2017/12/20/a-visual-guide-to-understanding-the-sign-in-javascript/)

2 Likes

I needed this. THANK YOU!

It seems that a variable can be assigned without using any of the keywords (var / let / const). For example:

name = ‘Bob’;
console.log(name);

seems to work as well as:

let name = ‘Bob’;
console.log(name);

And this seems to work fine, too:

holyCow = 42;
holyCow++
console.log(holyCow);

Is there any reason not to assign variables in this way (ie. without the keywords)?

At this point of learning JS the background reasoning is rather esoteric. For the time being it is best to follow good practice and always declare variables.

var a;        //  a is declared but not defined

var a = 42    //  a is declared and initialized (defined)

a = 42        //  a is defined but not declared

The latter can be deleted, while the others cannot.

As you progress through the course you will learn about functions and scope, so we won’t bring that up just now.


a = 42
console.log(a)    //  42
delete a
console.log(a)    //  ReferenceError: a is not defined
var a = 42
console.log(a)    //  42
delete a
console.log(a)    //  42
let a = 42
console.log(a)    //  42
delete a
console.log(a)    //  42
const a = 42
console.log(a)    //  42
delete a
console.log(a)    //  42

As we can see, declared variables are persistent, and delete has no effect. The last one above is not just persistent but immutable. Its value cannot be changed.

1 Like

İkinci dersin ikinci alıştırmasını birinci alıştırma gibi kodlamaya çalıştım, fakat hata veriyor. Verdiği hata ise şu; Did you create a variable named numOfSlices ? yani numOFSlices adında bir değişken oluşturup oluşturmadığımı soruyor. Rica etsem yardımcı olur musunuz?
Bu da benim kodlamam ama sistem bunu kabul etmiyor, mutlaka bir hatam söz konusu fakat bunu bilmiyorum, eğer biliyorsanız lütfen söyleyin. Teşekkürler.

var numOFSlices = 8;
console.log(numOFSlices);
//Output:8

case sensitivity, Of should have lowercase f