How do i give it a string


SyntaxError: Unexpected string

// Declare a variable on line 3 called
// myCountry and give it a string value.
var "myCountry".substring

// Use console.log to print out the length of the variable myCountry.

// Use console.log to print out the first three letters of myCountry.


This is where you went wrong:

var "myCountry".substring

A variable name cannot be in a string or written in numbers. Also a variable must be assigned to something to contain whatever you want in it.

So to correct this you would write.

var myCountry = "Country Name";


Declare a variable, myCountry

var myCountry;

Give it a string value. Note that strings are anything enclosed in quotes.

myCountry = "Canada";

Written in one statement, gives,

var myCountry = "Canada";

Now the string "Canada" is cached, and may be referred to at any time by the identifier (variable name), myCountry.

console.log(myCountry);    // Canada

Note that the variable does not have quotes. It is not a string, but what it refers to is.


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