Bird
Raised Fist0
Javascriptprogramming~5 mins

Writing first JavaScript program - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Writing first JavaScript program
Start Program
↓
Write code: console.log('Hello, world!')
↓
Run code in browser or Node.js
↓
JavaScript engine executes code
↓
Output appears on console: Hello, world!
↓
Program ends
The program starts, runs the console.log statement, prints the message, then ends.
Execution Sample
Javascript
console.log('Hello, world!');
Prints the text 'Hello, world!' to the console.
Execution Table
StepActionCode ExecutedOutput
1Start program
2Execute console.logconsole.log('Hello, world!');Hello, world!
3Program ends
💡 Program ends after printing the message.
Variable Tracker
VariableStartAfter Step 2Final
No variables---
Key Moments - 2 Insights
Why do we use console.log in this program?
console.log is used to show messages on the console. In the execution_table row 2, it prints 'Hello, world!' so we can see output.
Does this program change or store any data?
No, this program only prints a message. The variable_tracker shows no variables are created or changed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 2?
ANothing is printed
BGoodbye!
CHello, world!
DAn error message
💡 Hint
Check the Output column in execution_table row 2.
At which step does the program end?
AStep 3
BStep 1
CStep 2
DThere is no end
💡 Hint
Look at the Action column and exit_note in execution_table.
If we change the text inside console.log to 'Hi!', what changes in the execution_table?
AStep 2 action changes to something else
BOutput at step 2 changes to Hi!
CProgram ends earlier
DNo changes at all
💡 Hint
Focus on the Output column in execution_table row 2.
Concept Snapshot
JavaScript programs run code line by line.
Use console.log('text') to print messages.
The message appears in the console.
No variables needed for simple output.
Program ends after last line runs.
Full Transcript
This program shows how to write your first JavaScript code. It uses console.log to print 'Hello, world!' on the console. The execution flow starts the program, runs the console.log command, prints the message, then ends. No variables are created or changed. This is the simplest way to see output in JavaScript.

Practice

(1/5)
1. What does the console.log() function do in JavaScript?
easy
A. It changes the background color of the page.
B. It creates a pop-up alert on the screen.
C. It stops the program from running.
D. It shows messages in the browser console.

Solution

  1. Step 1: Understand the purpose of console.log()

    This function is used to display messages or values in the browser's console, which helps developers see output or debug.
  2. Step 2: Compare with other options

    Options A, B, and D describe different actions that console.log() does not perform.
  3. Final Answer:

    It shows messages in the browser console. -> Option D
  4. Quick Check:

    console.log() shows messages [OK]
Hint: Remember: console.log prints messages to the console [OK]
Common Mistakes:
  • Thinking console.log creates alerts
  • Confusing console.log with styling commands
  • Assuming console.log stops the program
2. Which of the following is the correct syntax to print "Hello, World!" in JavaScript?
easy
A. print("Hello, World!");
B. console.log("Hello, World!");
C. echo "Hello, World!";
D. System.out.println("Hello, World!");

Solution

  1. Step 1: Identify JavaScript syntax for output

    JavaScript uses console.log() to print messages to the console.
  2. Step 2: Check other options for language mismatch

    A is Python style, B is Java style, C is PHP style, so they are incorrect for JavaScript.
  3. Final Answer:

    console.log("Hello, World!"); -> Option B
  4. Quick Check:

    JavaScript output uses console.log() [OK]
Hint: Use console.log() to print messages in JavaScript [OK]
Common Mistakes:
  • Using print() instead of console.log()
  • Confusing JavaScript with other languages
  • Missing parentheses or semicolon
3. What will be the output of the following code?
console.log('Hi');
console.log('There');
medium
A. Hi There
B. Hi There
C. Hi\nThere
D. HiThere

Solution

  1. Step 1: Understand console.log behavior

    Each console.log() prints its message on a new line in the console.
  2. Step 2: Analyze the output lines

    The first prints "Hi", then the second prints "There" on the next line, so output is two lines: "Hi" then "There".
  3. Final Answer:

    Hi There -> Option A
  4. Quick Check:

    console.log prints each message on a new line [OK]
Hint: Each console.log prints on a new line automatically [OK]
Common Mistakes:
  • Thinking console.log joins messages on one line
  • Confusing \n as literal text instead of newline
  • Ignoring line breaks between calls
4. Find the error in this JavaScript code:
console.log('Hello World')
medium
A. console.log is misspelled
B. Incorrect quotes used
C. No error, code is correct
D. Missing semicolon at the end

Solution

  1. Step 1: Check syntax correctness

    The code uses correct function name, quotes, and parentheses.
  2. Step 2: Consider JavaScript semicolon rules

    Semicolons are optional at the end of statements in JavaScript, so missing semicolon is not an error.
  3. Final Answer:

    No error, code is correct -> Option C
  4. Quick Check:

    JavaScript allows optional semicolons [OK]
Hint: Semicolons are optional but recommended; no error here [OK]
Common Mistakes:
  • Thinking missing semicolon causes error
  • Confusing quote types as error
  • Assuming console.log is misspelled
5. You want to write a JavaScript program that prints your name and age on separate lines. Which code will do this correctly?
hard
A. console.log('Name: John'); console.log('Age: 25');
B. console.log('Name: John\nAge: 25');
C. console.log('Name: John' + 'Age: 25');
D. console.log('Name: John Age: 25');

Solution

  1. Step 1: Understand printing on separate lines

    Using two console.log() calls prints each message on its own line.
  2. Step 2: Check other options for line breaks

    console.log('Name: John\nAge: 25'); uses \n which creates a newline in the output; console.log('Name: John' + 'Age: 25'); joins to 'Name: JohnAge: 25' on one line; console.log('Name: John Age: 25'); prints all on one line.
  3. Final Answer:

    console.log('Name: John'); console.log('Age: 25'); -> Option A
  4. Quick Check:

    Separate console.log calls print on separate lines [OK]
Hint: Use separate console.log() for each line [OK]
Common Mistakes:
  • Using + without spaces causing joined text
  • Thinking \n creates a newline (prints literal \n)
  • Printing all info in one console.log without breaks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard