Why using "console.log"?


#1

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

Got past this exercise, but I wonder is it necessary to use console.log to print out results from substring method? As the substring method prints out the results by default!


Replace this line with your code. 
console.log("January".substring(0,3));
console.log("Melbourne is great".substring(0,12));
console.log("Hamburgers".substring(3,10));


#2

it return new String containing the extracted characters
string.substring(start,end) from start (including) until end (not including)


#3

Thanks, Shadiib

I understood that part but question why needs to use "console.log"?


#4

yes .... because you need to print the value that the method returns ....
it doesn't print by default


#5

you don't need console.log() to print out the string values. Its just for the formality of the exercise.

The code editor you type into by default is a console that you're constantly logging values into. So regardless,
it still works either way.

And one more thing. Why you would want to use a console.log() : Since there are different existing variables with accessible values often created while programming, you'll need to use a console.log() to specifically print out the result you want.

For example.
you create a variable in your console or programming editor.
and you store the value 2 inside it.

var x = 2;
if you type "x" in the console, it print out the value 2 for you. Because it knows you stored two inside it.
But what if in your programming editor have to values;
var x = 3;
var y = 4;
Now the console will ignore the first line of code which is 3 and print 4
why? Because computers read programs line by line.
so it will read line 3 then jump and print out line 4
That means in your console you get the value "Y". what is equal to 4 as your result .

Now if you're being specific and you want to get a result of, say 3 + 4;
then you have to use the console.log() function to get the result
console.log(y + x);

So far, we can say that console.log allows us to be more specific with the type of result output we want not withstanding the infinite number of existing values.


#6

Hi Sandra,


#7

Thanks for the answer. It is really helpful. You rock!


#8

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