Variables in JS


#1

How to do this?
"Use console.log to print out the length of the variable myCountry."


#2

If you are on the lookout ....

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

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

====================================

You would have defined a variable myCountry
to which you assign a string Value
like
var myCountry = "tessa-land";

From now on,
if you use myCountry
the Javascript-interpreter will evaluate
the myCountry-variable to the string Value ="tessa-land"=

Javascript gives you a set of properties and pre-defined-functions/methods
under the String.prototype
= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/prototype
Look in the left-hand column....
You will then have to follow the required-syntax...

With the console.log() method,
you are able to Display a result in the console which is in the right-hand-preview-screen.
= https://developer.mozilla.org/en-US/docs/Web/API/Console/log

console.log( myCountry.length );


#3

Here's where I'm confused. If we declare the variable myCountry to be "tessa-land" & then subsequently console.log the length, why do we have to put the string value "tessa-land" in the parenthesis? Why wouldn't the JS recognize myCountry as tessa-land & put that in the parenthesis?

correct example:

var myCountry = "United States of America";
console.log("United States of America" .length );

why isn't it:

var myCountry = "United States of America";
console.log("myCountry" .length );

Maybe I'm thinking too much about SASS / SCSS & I'm missing something.
Is JS simply reading the string values inside the parenthesis & nothing else, not the machine name?
What's the point of declaring a var name if you have to put the full name when you call it up for a console.log?
I'm sure there's a million reasons & I haven't covered them yet.

Thanks.


#4

@drisone78,

If you define a variable using the var-keyword
and you assign a specific Value to this variable

The VALUE can be anything from:

•Number
•String
•Boolean
•Symbol
•Object  ◦Function/'Method'
         ◦Array
         ◦Date
         ◦RegExp
         ◦Math
•Null
•Undefined

In our case that would be a string Value
var myCountry = "United States of America";

Reference::

google search
=the book=
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