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
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:
- Writing statements the computer accepts.
- 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
| Where | How | Best 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 REPL | Install 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:
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.
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:
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:
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");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.
| Line | What happens | Console so far |
|---|---|---|
| 1 | print text | Wake up |
| 2 | print text | Wake up · Brush teeth |
| 3 | calculate 2 * 3 = 6, print it | … · 6 |
| 4 | print 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.
// 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:
console.log("Start");consol.log("Oops"); // typo: consol instead of consoleconsole.log("End");Start
ReferenceError: consol is not defined
at Object.<anonymous> (/Users/you/dsa/hello.js:2:1)Read it in three parts:
- What already ran:
Startwas printed, so line 1 was fine. - The error type and message:
ReferenceError: consol is not defined. This means “you used a name I do not know”. - Where:
hello.js:2:1means filehello.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:
| Error | Usually means | Example |
|---|---|---|
ReferenceError | A name that is misspelt or does not exist | consol.log, Console.log (capital C) |
SyntaxError | The code breaks the rules of JavaScript, so the program does not run at all | A missing quote or bracket: console.log("Hi) |
TypeError | A value was used in a way that its kind (its type) does not allow | Calling 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 |
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.
Write a program that prints Hello, World!.
Hint 1
Hint 2
console.log("…").Show all 3 solutions with explanations
Put the whole sentence inside one pair of quotes and print it.
console.log("Hello, World!"); // Hello, World!One statement, one line of output. The computer prints the text exactly as written, so check every character.
console.log can print several values. It puts one space between them.
console.log("Hello,", "World!"); // Hello, World!The first value is "Hello," and the second is "World!". The space between them is added automatically.
Between two pieces of text, + joins them into one longer piece of text. This is called concatenation.
console.log("Hello, " + "World!"); // Hello, World!The space is inside the first piece of text ("Hello, "). With +, no space is added for you, so you must include it yourself.
Approach 1 is the simplest and clearest. The other two show tools you will use often: printing several values, and joining text.
Print your name, your city and what you are learning, each on its own line.
My name is Asha I live in Pune I am learning DSA
Hint 1
console.log prints one line. How many lines do you need?Hint 2
console.log statements, one after the other.Show all 2 solutions with explanations
One statement per line of output.
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*/The computer runs the statements from top to bottom, and every console.log ends with a new line.
Inside text, the two characters \n mean “start a new line here”.
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*/\n is called the newline character. It is not printed. It moves the output to the next line.
Use three statements while learning — it is easier to read. Knowing \n is still useful, because you will see it in many programs.
Without running it, predict what each line prints. Then run it and check.
console.log(2 + 3);console.log("2 + 3");console.log("2" + "3");Hint 1
Hint 2
+ adds. Quotes → text → + joins.Show all 2 solutions with explanations
Decide the type of each value first, then apply the rule for +.
console.log(2 + 3); // 5console.log("2 + 3"); // 2 + 3console.log("2" + "3"); // 23| Line | Values are | + means | Prints |
|---|---|---|---|
| 2 + 3 | numbers | add | 5 |
| "2 + 3" | one piece of text | (nothing to do) | 2 + 3 |
| "2" + "3" | text and text | join | 23 |
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.
console.log(typeof (2 + 3)); // numberconsole.log(typeof "2 + 3"); // stringconsole.log(typeof ("2" + "3")); // stringA string is a value that is text. The third result is a string, which confirms that + joined the two pieces of text into "23".
In an interview you will reason it out (Approach 1). When you are unsure while practising, typeof gives a quick, certain answer.
What is printed, and in what order?
console.log("C");console.log("A");console.log(10 - 3);console.log("B");C A 7 B
Hint 1
Hint 2
Show the solution with explanations
Go through the lines one by one and write down what each one prints.
console.log("C");console.log("A");console.log(10 - 3);console.log("B");/* Output:CA7B*/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.
This line does not run. Find the problem and fix it so it prints Hello.
Hint 1
Hint 2
).Show all 2 solutions with explanations
The text opens with " but never closes. Add the closing ".
console.log("Hello"); // HelloWithout 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).
Text can also be written between single quotes. Both quotes must be the same kind.
console.log('Hello'); // Hello"Hello" and 'Hello' are exactly the same text. Just never mix them. "Hello' is still an error.
Either fix is correct. Pick one style of quote and use it everywhere in your code. This series uses double quotes.
This prints ReferenceError: Console is not defined. Why, and how do you fix it?
Hint 1
Hint 2
Show the solution with explanations
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.
console.log("Hi"); // HiConsole and console are two different names to JavaScript. Only console exists, so Console causes a ReferenceError, which means “I do not know this name”.
Print this shape exactly. The middle line has 3 spaces between the stars.
****** *********** * * *****
Hint 1
console.log statements you need.Hint 2
"* *".Show all 2 solutions with explanations
Write each line of the shape as its own piece of text.
console.log("*****");console.log("* *");console.log("*****");/* Output:****** *******/The middle line needs exactly 3 spaces so that its stars line up with the edges of the top and bottom lines.
The top and bottom lines are the same. Write that text once, give it a name, and print it twice.
const edge = "*****";const middle = "* *";console.log(edge);console.log(middle);console.log(edge);/* Output:****** *******/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.
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
- Write, from memory, a program that prints three lines, and run it with
node. - Explain to yourself (out loud is best) why
"2" + "3"prints 23. - 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