Part 1 · Lesson 1 · 12 min read

What a Program Is, and How to Run One

A program is a list of instructions run top to bottom. Run your first JavaScript, read its output, and learn to predict exactly what prints.

On this page

  1. A program is a recipe
  2. Why this matters for DSA interviews
  3. Three ways to run JavaScript
  4. console.log — making the program talk
  5. Top to bottom, one line at a time
  6. Comments — notes the computer ignores
  7. Reading an error message
  8. The habit: predict, then run
  9. Practice questions (7)
  10. Make it stick

A program is a recipe

A recipe is a list of steps: boil water, add rice, wait ten minutes. You follow them in order, one at a time, exactly as written. A program is the same thing for a computer: a list of instructions, called statements, that the computer follows from the first line to the last.

The computer follows your instructions exactly. It does what you wrote, not what you meant. If an instruction contains a spelling mistake, the computer stops and reports an error. Errors are normal, and in this lesson you will learn how to read them.

This series uses JavaScript. JavaScript is a programming language: a set of words and rules for writing instructions that a computer can follow. It runs in every web browser. It also runs on any computer with Node.js (a free program that runs JavaScript outside the browser). Its rules (called syntax) look like the rules of other common interview languages, such as Java, C++ and Python. So every idea you learn here also helps you in those languages.

Why this matters for DSA interviews

DSA means data structures and algorithms. A data structure is a way to store information so it is easy to use (for example, a list). An algorithm is a set of clear steps that solves a problem. DSA sounds big, but every interview question has the same shape: here is some input, write a small program that gives the right output. Before you can learn clever methods, you need two basic skills:

  1. Writing statements the computer accepts.
  2. Predicting exactly what a program will print, line by line, before you run it. This is called a dry run (you act like the computer and follow the code by hand). It is the skill this whole series is built on.

Interviewers often ask “what does this code print?” or “walk me through your code with this example”. If you can dry-run, you can answer both.

Three ways to run JavaScript

WhereHowBest for
Browser console (a small window inside the browser where you can type JavaScript)Open Chrome, press F12 (or Cmd+Option+J on Mac), click Console, type a line, press Enter.Trying one line right now, with nothing to install.
Node.js REPLInstall Node.js (choose the LTS version from nodejs.org; LTS means “long-term support”, the stable version). Open a terminal (a window where you type commands) and type node. This opens the REPL: a place that reads one line, runs it, and shows the result.Quick experiments. Type .exit to leave.
A file (recommended)Write code in a file like hello.js in VS Code, then in the terminal run node hello.js.Everything in this series. You can edit, save and run again.

Create a file named hello.js, put this one line in it, save it, and run node hello.js in the terminal:

JavaScript
console.log("Hello, World!");

The terminal shows Hello, World!. You have written and run a program. Every program in this series is run the same way.

Online, with nothing installed

If you can't install anything yet, any online JavaScript playground (search “JavaScript online compiler”) or LeetCode's own editor works too (LeetCode is a website with coding practice questions). At the very least, use the browser console. But move to files soon. Interviews expect you to write a whole function (a named, reusable block of code), not just one line.

console.log — making the program talk

console.log(something) is a statement that prints something on its own line. The console is the place where output appears (the terminal or the browser console). Printing is how a program shows you what it is doing. It is also your main tool for debugging (finding and fixing mistakes) in this series. The rules:

JavaScript
console.log("I am learning DSA");   // text goes inside quotesconsole.log(42);                     // numbers do not need quotesconsole.log(10 + 5);                 // JavaScript calculates first: 15console.log("10 + 5");               // inside quotes it is just textconsole.log("Total:", 10 + 5);       // several values, separated by a space/* Output:I am learning DSA421510 + 5Total: 15*/
  • Text goes inside quotes — "double" or 'single', both work. Text inside quotes is called a string. Without quotes, JavaScript thinks you mean the name of something.
  • Numbers need no quotes, and maths is calculated before printing.
  • Quotes turn maths into text: "10 + 5" prints exactly those characters.
  • Commas print several values on one line, with a space between them.
  • Each console.log starts a new line.

Top to bottom, one line at a time

The computer runs statement 1, then 2, then 3. It never jumps ahead. It also never goes back until you learn loops (a loop repeats lines of code; you will learn it in Lesson 4). Step through the code with the buttons. Watch the highlighted line and the console:

step 1 / 6
Set up
The computer starts at line 1

A program always starts at the first line and moves down. Nothing has been printed yet.

▶console.log("Wake up");2console.log("Brush teeth");3console.log(2 * 3);4console.log("Go to work");
Variables
— none yet —
Console
(nothing printed yet)
Each press of Next runs one line. The console only grows downward, in the same order as the code. · interactive

You can write the same steps on paper. This is a dry-run table: one row for each step, showing what happens and what has been printed so far.

Dry run · the morning program
LineWhat happensConsole so far
1print textWake up
2print textWake up · Brush teeth
3calculate 2 * 3 = 6, print it… · 6
4print text… · 6 · Go to work

A table may seem unnecessary for four lines. Once you start using loops, it is the most reliable way to check your understanding.

Comments — notes the computer ignores

A comment is text in your code that the computer ignores. Anything after // on a line is a comment. Anything between /* and */ is also a comment. Use comments to explain why you did something. You can also use them to switch a line off for a while without deleting it.

JavaScript
// This whole line is a comment. The computer skips it.console.log("This runs");   // a comment can also sit at the end of a line/*  A multi-line comment.  Useful for longer explanations.*/// console.log("This does NOT run — it has been 'commented out'");/* Output:This runs*/

In this series, the expected output is often shown in an /* Output: … */ comment at the bottom of the code, so you can check your prediction.

Reading an error message

An error is not a failure. It is a message from the computer that tells you where the problem is. Run this:

JavaScript
console.log("Start");consol.log("Oops");   // typo: consol instead of consoleconsole.log("End");
What the terminal shows
Start
ReferenceError: consol is not defined
    at Object.<anonymous> (/Users/you/dsa/hello.js:2:1)

Read it in three parts:

  1. What already ran: Start was printed, so line 1 was fine.
  2. The error type and message: ReferenceError: consol is not defined. This means “you used a name I do not know”.
  3. Where: hello.js:2:1 means file hello.js, line 2, column 1 (the first character on the line).

Notice that End was never printed. When a program hits an error, it stops there. These are the three errors you will meet most at the start:

ErrorUsually meansExample
ReferenceErrorA name that is misspelt or does not existconsol.log, Console.log (capital C)
SyntaxErrorThe code breaks the rules of JavaScript, so the program does not run at allA missing quote or bracket: console.log("Hi)
TypeErrorA value was used in a way that its kind (its type) does not allowCalling something that is not a function: console.lg("Hi"). console.lg does not exist, so its value is undefined (JavaScript's word for “nothing here”), and undefined cannot be called
JavaScript is case-sensitive

console, Console and CONSOLE are three different names. Only the first one exists.

The habit: predict, then run

Every time you see code in this series — before you run it, before you open an answer — write down what you think it prints. Then run it and compare. When you are wrong, you learn the most. Find the exact line where your prediction and the real result were different. Do this for a few weeks and you will be able to “run” code in your head. This is exactly what an interviewer wants to see.

Practice questions

Try each one in a file before you open the answer. They start very easy on purpose. The goal is to become comfortable writing and running code.

Q1Hello, WorldEasy

Write a program that prints Hello, World!.

Example 1
Input: (no input)
Output: Hello, World!
Explanation: The program prints exactly the characters between the quotes: capital H, a comma, a space, capital W and an exclamation mark.
Stuck? Open one hint at a time
Hint 1
Which statement prints something on the screen? You saw it in the “console.log” section.
Hint 2
Text must go inside quotes: console.log("…").
Show all 3 solutions with explanations
Approach 1One piece of text
The idea

Put the whole sentence inside one pair of quotes and print it.

JavaScript
console.log("Hello, World!"); // Hello, World!
How it works

One statement, one line of output. The computer prints the text exactly as written, so check every character.

Approach 2Two values separated by a comma
The idea

console.log can print several values. It puts one space between them.

JavaScript
console.log("Hello,", "World!"); // Hello, World!
How it works

The first value is "Hello," and the second is "World!". The space between them is added automatically.

Approach 3Join two pieces of text with +
The idea

Between two pieces of text, + joins them into one longer piece of text. This is called concatenation.

JavaScript
console.log("Hello, " + "World!"); // Hello, World!
How it works

The space is inside the first piece of text ("Hello, "). With +, no space is added for you, so you must include it yourself.

Which approach should you use?

Approach 1 is the simplest and clearest. The other two show tools you will use often: printing several values, and joining text.

Q2Introduce yourself in three linesEasy

Print your name, your city and what you are learning, each on its own line.

Example 1
Input: (no input)
Output:
My name is Asha
I live in Pune
I am learning DSA
Explanation: Each sentence appears on its own line, in the same order as in the code.
Stuck? Open one hint at a time
Hint 1
Each console.log prints one line. How many lines do you need?
Hint 2
Write three console.log statements, one after the other.
Show all 2 solutions with explanations
Approach 1Three console.log statements
The idea

One statement per line of output.

JavaScript
console.log("My name is Asha");console.log("I live in Pune");console.log("I am learning DSA");/* Output:My name is AshaI live in PuneI am learning DSA*/
How it works

The computer runs the statements from top to bottom, and every console.log ends with a new line.

Approach 2One console.log with \n
The idea

Inside text, the two characters \n mean “start a new line here”.

JavaScript
console.log("My name is Asha\nI live in Pune\nI am learning DSA");/* Output:My name is AshaI live in PuneI am learning DSA*/
How it works

\n is called the newline character. It is not printed. It moves the output to the next line.

Which approach should you use?

Use three statements while learning — it is easier to read. Knowing \n is still useful, because you will see it in many programs.

Q3Maths or text?Easy

Without running it, predict what each line prints. Then run it and check.

JavaScript
console.log(2 + 3);console.log("2 + 3");console.log("2" + "3");
Example 1
Input: console.log(2 + 3);
Output: 5
Explanation: There are no quotes, so these are numbers. + adds them.
Example 2
Input: console.log("2 + 3");
Output: 2 + 3
Explanation: Everything is inside one pair of quotes, so it is text and is printed exactly as written.
Example 3
Input: console.log("2" + "3");
Output: 23
Explanation: Both values are text. Between text, + joins instead of adding.
Stuck? Open one hint at a time
Hint 1
Look at each line and ask: are the values inside quotes?
Hint 2
No quotes → numbers → + adds. Quotes → text → + joins.
Show all 2 solutions with explanations
Approach 1Reason it out line by line
The idea

Decide the type of each value first, then apply the rule for +.

JavaScript
console.log(2 + 3);     // 5console.log("2 + 3");   // 2 + 3console.log("2" + "3"); // 23
How it works
Dry run · three lines
LineValues are+ meansPrints
2 + 3numbersadd5
"2 + 3"one piece of text(nothing to do)2 + 3
"2" + "3"text and textjoin23
Approach 2Check the type with typeof
The idea

typeof is an operator that tells you the type of a value (what kind of value it is, such as number or text). Use it to confirm your answer.

JavaScript
console.log(typeof (2 + 3));     // numberconsole.log(typeof "2 + 3");     // stringconsole.log(typeof ("2" + "3")); // string
How it works

A string is a value that is text. The third result is a string, which confirms that + joined the two pieces of text into "23".

Which approach should you use?

In an interview you will reason it out (Approach 1). When you are unsure while practising, typeof gives a quick, certain answer.

Q4Predict the orderEasy

What is printed, and in what order?

JavaScript
console.log("C");console.log("A");console.log(10 - 3);console.log("B");
Example 1
Input: the program below
Output:
C
A
7
B
Explanation: The lines print in the order they are written. Line 3 has no quotes, so 10 − 3 is calculated and 7 is printed.
Stuck? Open one hint at a time
Hint 1
The computer always runs the first line first, then the second, and so on.
Hint 2
Which line contains maths instead of text?
Show the solution with explanations
Approach 1Follow the program from top to bottom
The idea

Go through the lines one by one and write down what each one prints.

JavaScript
console.log("C");console.log("A");console.log(10 - 3);console.log("B");/* Output:CA7B*/
How it works

The order of the output always follows the order of the statements. It does not follow the alphabet or anything else. Only line 3 calculates something.

Q5Fix the SyntaxErrorEasy

This line does not run. Find the problem and fix it so it prints Hello.

Example 1
Input: console.log("Hello);
Output: Hello
Explanation: After the fix, the program prints the word Hello. Before the fix, it prints a SyntaxError and nothing else runs.
Stuck? Open one hint at a time
Hint 1
Count the quotes in the line. How many are there?
Hint 2
Every opening quote needs a matching closing quote before the ).
Show all 2 solutions with explanations
Approach 1Add the missing double quote
The idea

The text opens with " but never closes. Add the closing ".

JavaScript
console.log("Hello");  // Hello
How it works

Without the closing quote, JavaScript keeps reading until the end of the line, looking for the end of the text. It never finds it. The code breaks the rules of JavaScript, so you get a SyntaxError (an error about wrong code shape).

Approach 2Use single quotes instead
The idea

Text can also be written between single quotes. Both quotes must be the same kind.

JavaScript
console.log('Hello');  // Hello
How it works

"Hello" and 'Hello' are exactly the same text. Just never mix them. "Hello' is still an error.

Which approach should you use?

Either fix is correct. Pick one style of quote and use it everywhere in your code. This series uses double quotes.

Q6Fix the ReferenceErrorEasy

This prints ReferenceError: Console is not defined. Why, and how do you fix it?

Example 1
Input: Console.log("Hi");
Output: Hi
Explanation: After the fix, the program prints Hi. Before the fix, it prints: ReferenceError: Console is not defined.
Stuck? Open one hint at a time
Hint 1
Read the error message: which name does it say is “not defined”?
Hint 2
Compare that name with the one used in every other example. Look at the first letter.
Show the solution with explanations
Approach 1Correct the capital letter
The idea

JavaScript is case-sensitive, which means a capital letter and a small letter are different. The built-in name is console, with a small c.

JavaScript
console.log("Hi");  // Hi
How it works

Console and console are two different names to JavaScript. Only console exists, so Console causes a ReferenceError, which means “I do not know this name”.

Q7Draw a boxEasy

Print this shape exactly. The middle line has 3 spaces between the stars.

TEXT
******   ******
Example 1
Input: (no input)
Output:
*****
*   *
*****
Explanation: Three lines. The top and bottom lines have 5 stars. The middle line is a star, 3 spaces, and a star.
Stuck? Open one hint at a time
Hint 1
How many lines does the shape have? That is how many console.log statements you need.
Hint 2
The first and last lines are identical. The middle line is "* *".
Show all 2 solutions with explanations
Approach 1One console.log per line
The idea

Write each line of the shape as its own piece of text.

JavaScript
console.log("*****");console.log("*   *");console.log("*****");/* Output:******   *******/
How it works

The middle line needs exactly 3 spaces so that its stars line up with the edges of the top and bottom lines.

Approach 2Store the repeated line once
The idea

The top and bottom lines are the same. Write that text once, give it a name, and print it twice.

JavaScript
const edge = "*****";const middle = "*   *";console.log(edge);console.log(middle);console.log(edge);/* Output:******   *******/
How it works

edge and middle are variables — named boxes that hold a value. You will learn them properly in Lesson 2. If the text ever needs to change, you change it in one place.

Which approach should you use?

Both are correct. Approach 2 avoids writing the same thing twice. This matters more as programs grow. In Lesson 6 you will draw boxes of any size with loops.

Make it stick

Make it stick — close this page, then
  1. Write, from memory, a program that prints three lines, and run it with node.
  2. Explain to yourself (out loud is best) why "2" + "3" prints 23.
  3. Break a working program on purpose — remove a quote, misspell console — and read each error: type, message, line number.

Revisit schedule: solve two of the questions again tomorrow, again in 3 days, and once more in a week — without looking at the answers. Each time it will feel easier. That is the memory forming.

Next lesson: storing values in variables (named boxes that hold a value), and the operators (symbols like + and % that work on values). The % operator is used in almost every DSA problem.

Published by Jainik · Last updated · How these lessons are made

Keep learning

Related series