Create a variable: const, quotes or no quotes?


#1

Does it matter if there are single quotes or no quotes around the variable?

When I ran my own code with single quotes around each of these variables, all of the variables printed to the console.
Example:

const myAge = ‘22’;
console.log(myAge);
const hairColor = ‘black’;
console.log(hairColor);
const shoeSize = ‘5.5’;
console.log(shoeSize);
const myCar = ‘HondaHRV’;
console.log(myCar);
// Output:
22
black
5.5
HondaHRV

HOWEVER, when I removed the single quotes around the variable, only the variable 22 was printed to the console. The rest of the code printed to the console as an error message:
const myAge = 22;
console.log(myAge);
const hairColor = black;
console.log(hairColor);
const shoeSize = 5.5;
console.log(shoeSize);
const myCar = HondaHRV;
console.log(myCar);
// Output:

22
/home/ccuser/workspace/learn-javascript-variables-const/app.js:3
const hairColor = black;
^

ReferenceError: black is not defined
at Object. (/home/ccuser/workspace/learn-javascript-variables-const/app.js:3:19)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

Can anyone explain to me why this is?

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/courses/learn-javascript-introduction/lessons/variables/exercises/const?action=lesson_resume&course_redirect=introduction-to-javascript

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

22
/home/ccuser/workspace/learn-javascript-variables-const/app.js:3
const hairColor = black;
^

ReferenceError: black is not defined
at Object. (/home/ccuser/workspace/learn-javascript-variables-const/app.js:3:19)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

```

Replace this line with your code.

<do not remove the three backticks above>

#2

yes it does matter :slight_smile:

we use "" or '' for string value. Anything inside quotation marks consider as a string.

Don’t use quotation marks if you want a value as a number.

for example:

'22'(with '') is string value but 22(without '') is a number.

now question is why are you getting an error for

const hairColor = black;

but not for

const myAge = 22;

It’s because javascript doesn’t know what is black.black can’t be number but 22 can be.


#3

Thank you! :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.