Hello, JavaScript
Run JavaScript in the browser and in Node.js, and print your first output.
What you will learn
- Run code in the console and Node
- Use console.log
- Understand where JS runs
JavaScript began as a small scripting language for web pages and now runs almost everywhere: every browser, servers through Node.js, mobile apps and desktop tools. It is the only language browsers run natively, so anyone who builds for the web meets it sooner or later.
Two places to run it
- The browser console: open developer tools (F12, or Cmd+Option+J on a Mac) and type code straight in. Nothing to install.
- Node.js: install it from nodejs.org, save code in a file and run it from the terminal.
console.log("Hello, stackcone!");
console.log(2 + 2);
console.log("2 + 2 =", 2 + 2);Hello, stackcone! 4 2 + 2 = 4
node hello.jsStatements, semicolons and comments
Statements usually end with a semicolon. JavaScript can insert them automatically, but relying on that occasionally causes surprises, so pick a style and stay consistent. Comments use // for one line and /* ... */ for blocks.
// a single-line comment
const greeting = "hi"; /* inline block comment */
console.log(greeting);JavaScript is not Java
The names are a marketing accident. The languages are unrelated: JavaScript is dynamically typed and runs in an engine (V8 in Chrome and Node), while Java is compiled and statically typed.
Common mistakes
- Forgetting quotes around text, giving
ReferenceError: Hello is not defined. - Typing
Console.logwith a capital C; names are case sensitive. - Ignoring the console: red errors there almost always tell you the file, line and cause.
Try it yourself
Print your name, then your favorite number multiplied by 3, on two separate lines.
Show solution
console.log("Amar");
console.log(7 * 3);