⬅ Previous Topic
Setting Up JavaScript EnvironmentNext Topic ⮕
JavaScript in the Browser vs Node.js⬅ Previous Topic
Setting Up JavaScript EnvironmentNext Topic ⮕
JavaScript in the Browser vs Node.jsWelcome to JavaScript! In this lesson, you will write your first JavaScript program. We’ll begin with simple instructions that run in the browser console or Node.js, and build your confidence with real, working examples.
Before you start, you can run JavaScript in two common ways:
Ctrl+Shift+J
or Cmd+Option+J
(Mac) to open Developer Tools and go to the Console tab.The classic first program in any language prints a message to the screen. In JavaScript, we use console.log()
to output messages.
console.log("Hello, World!");
Output:
Hello, World!
The console.log()
function sends output to the browser's console or the terminal in Node.js. This is extremely useful for debugging and seeing what your program is doing.
Can you print numbers or variables too?
Answer: Yes! You can print anything: strings, numbers, variables, objects, arrays, etc.
console.log(42);
console.log(3.14);
Output:
42 3.14
JavaScript supports variable declarations using let
, const
, and var
. Let’s create a variable and print it.
let name = "Alice";
console.log("Hello, " + name + "!");
Output:
Hello, Alice!
Let’s perform a basic addition and print the result:
let a = 5;
let b = 7;
let sum = a + b;
console.log("The sum is:", sum);
Output:
The sum is: 12
What happens if you use const
instead of let
?
Answer: const
creates a constant variable. Once assigned, its value cannot be changed.
const pi = 3.14159;
console.log("The value of pi is", pi);
Output:
The value of pi is 3.14159
console.log()
liberally to understand what your code is doing.You’ve written your first JavaScript program! You've learned how to print text and numbers, use variables, and even perform simple calculations. This is your foundation. In the next lessons, we’ll explore data types, operators, and control flow to build your programming skills further.
⬅ Previous Topic
Setting Up JavaScript EnvironmentNext Topic ⮕
JavaScript in the Browser vs Node.jsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.