How do i give it a string


#1

https://www.codecademy.com/courses/getting-started-v2/4/2?curriculum_id=506324b3a7dffd00020bf661

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.
console.log("myCountry".length);

// Use console.log to print out the first three letters of myCountry.
console.log("myCountry".substring(0,3));


#2

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";


#3

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.


#4

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