When do you use console.log?


#1

When do you use console.log?


#2

Whenever you wish to log some output to the console. Say we want to print a list...

var birds = [ 'sparrow', 'robin', 'swallow', 'crow', 'magpie', 'chickadee']

for (var i = 0; i < birds.length; i++) {
    console.log(birds[i]);
}

sparrow
robin
swallow
crow
magpie
chickadee

We may wish to log the result of a computation done by a function...

var factorial = function (x) {
    p = x;
    while (--x > 0) {
        p *= x;
    }
    return p;    
};

console.log(factorial(7));    // 5040

#3


hi guys! i'm a newbie and doesn't know everything yet in javascript... can you tell me what's the difference between console.log and document.write, when to use console.log and when to usedocument.write??



#4

We use console.log() when we want to log output to the debugging console. The rather outdated, document.write() is used to output HTML and raw text to the browser window. For the time being, set this aside and pour yourself into the introductory lessons of the JS track. Most focus is on the console, since we aren't working with HTML in the module.


#5

Some examples you can find here: JavaScript Output


#6

:slight_smile: thank you :slight_smile:


#7

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