This forum is now read-only. Please use our new forums! Go to forums
banner
Close banner
print vs. console.log
Not sure if this is a glitch or what, but when I run this simple “greet” function using a print command instead of a console.log command, it prints the variable twice.
var greeting = "Ahoy";
var greet = function (){;
print(greeting)
};
greet();
But if I run it with console.log, it just prints “Ahoy” once.
var greeting = "Ahoy";
var greet = function (){;
console.log(greeting)
};
greet();
What’s the difference? Why would it do that?
Answer 50812c728643780200002e7a
3 votes
JavaScript does not have a print
method – when I try it in Google Chrome’s, Opera’s or Firefox’s Developer console, it opens the print dialog, you know, the one for sending a Web page to a physical printer.
So the correct way of solving this is definitely your latter example.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency