25. More Variable Practice


#1

How come in the tutorial:
"Steve Jobs".substring(0,5); evaluates to Steve

But when I input this exact string into the script.js area and submit it, I get "Steve" (in quotes)?

What is the difference?


#2

If we log output directly, string literals are unquoted. JavaScript sees the quotes as delimiters and only prints what is between them.

If we do not log output, the JavaScript console responds to the last ‘command’ (which we see as a program statement) by echoing the last object in its buffer. If it is a string, it will be echoed as a property value with quotes.

Try it in labs repl.it

console.log("Direct logging");
str = "Response echo";

Output

 Direct logging
=> 'Response echo'

#3

If you played in the lab on the previous post, you will have noticed the difference between its console display and the Exercise IDE display, namely, prompt characters, > and response characters, =>, and there is color highlighting so we can see the direct output in white, and console output in green.

The biggest difference, of course is the command line in orange.


#4

thank you! How come 'Response echo' comes out in single quotes in the lab though? I've only seen double quotes output here in Codecademy.


#5

Single quotes are typical for the compiler. We can use either, but when outputing a string or array with string data, expect it to appear as single. Not an NB point to concern over, as long as we use quotes correctly in any given situation.

Everyone has their own take on quotes, myself included. I'm not that anal about it, and tend to get sloppy at times when I'm not vigilante, but on the whole, I give HTML attributes first dibs on double quotes. If my code is generating an HTMLString, it gets written in single quotes so that I can use the doubles on attributes:

Eg.

obj.innerHTML = '<div class="header"><h1>Heading</h1></div>';

Other normal preferences for some are string primitives always in double quotes, and in single quotes when passed as arguments. Again, this is non-fundamental so shouldn't be cast in stone. In time you will get very used to working with string objects in the many settings where they occur.


#6

Thanks for the clear explanation!