10

I am learning JavaScript for the first time using NetBeans. My code can compile, but I don't know how to print to the console screen. I've tried the System.out.println function, but that still doesn't work. What am I doing wrong?

Enter image description here

At all the other programs I've used online, the output was automatic, so to add "console.log()", where do I put it, and how do I make it show the values of the variables like in the text?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sloan Brown
  • 225
  • 1
  • 2
  • 5
  • 3
    Possible duplicate of [JavaScript: How do I print a message to the error console?](http://stackoverflow.com/questions/164397/javascript-how-do-i-print-a-message-to-the-error-console) –  Jan 12 '16 at 00:21
  • 2
    That does look very like Resig stuff. Are you from Khanacademy? Methods like fill & textSize are not native in javascript. It's a part of a library. – Fella Jan 12 '16 at 00:23
  • ok guys, i get it, problem is there is no question here that shows me how i actually USE the javascript!= function. EX: Where do I put it? Do I put Javascript != { and then code? or....what? I've never used it so, maybe i'm nuking it, but i'm trying to use it and It's not working.....neither it nor the console.log is not working because i have no idea how to use those things. – Sloan Brown Jan 12 '16 at 00:49
  • It probably shouldn't be tagged with NetBeans. The screenshot shows a file with file extensions ".js" (JavaScript). Perhaps NetBeans is just the [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) or text editor that is used for the JavaScript file? Mentioning NetBeans ***at all*** may cause unnecessary confusion. But the OP has left the building: *"Last seen more than 1 year ago"* – Peter Mortensen Sep 25 '22 at 13:05
  • From the [NetBeans article](https://en.wikipedia.org/wiki/NetBeans#NetBeans_JavaScript_editor): *"The NetBeans JavaScript editor provides extended support for JavaScript, Ajax, and CSS."*. So NetBeans may no longer imply Java (though it may still be the default association). – Peter Mortensen Sep 25 '22 at 13:13

3 Answers3

25

That looks like you're confusing JavaScript with Java. They're not the same language. NetBeans is a development environment for Java, not JavaScript. But to answer your main question, to print to the console in JavaScript, you can use the function console.log() like this.

    console.log(text);

In your case, you could write

    console.log("Obama is " + obama.age + " years old.");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
uniqueusername
  • 440
  • 1
  • 5
  • 9
  • 1
    *"confusing JavaScript with Java"* probably refers to the mentioned "System.out.println" function being [a Java function](https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#out). – Peter Mortensen Sep 25 '22 at 13:34
14

Use console.log("some text") to print the text

(or)

If you want to print the value stored in the variable then you can give the variable name inside the console.log()

e.g.

var text = "some text"
console.log(text)
Manikandan C
  • 668
  • 1
  • 9
  • 22
1

A not-so-good solution is a simple link. Print with console.log:

let print = console.log
print('Hello, World!')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131