Introduction to JavaScript
Published by Brayan K
JavaScript is a high-level programming language that runs in every web browser, adding interactivity and behavior to websites, and also powers servers, apps, and tools through runtimes like Node.js.
Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Make one visible change
Start with a small receipt, not a page full of syntax. Open the example, press Run, and compare the console with the checked panel. Then change the quantity in the multiplication and run it again. Editing the code alone does not execute it.
// Three notebooks, each costing four units.
console.log("Notebook order");
console.log(3 * 4);
console.log("Ready to pack");
// ✅ Expected output:
// Notebook order
// 12
// Ready to packconsole.log sends a value to the console. Parentheses hold what you want to inspect; the semicolon finishes the statement. The words inside quotes are a string: text to keep as text. The multiplication is an expression: JavaScript calculates its value before giving that value to console.log.
The console is an observation tool. It does not create a heading, update a shopping basket or put a message on the webpage. HTML supplies page structure, CSS supplies presentation, and JavaScript can change the page through browser APIs. DOM Manipulation teaches that page-changing step. Here, the console lets you concentrate on what your program does.
Text that looks like code is still text
console.log("3 * 4");
console.log(3 * 4);
console.log('Ready to pack');
// ✅ Expected output:
// 3 * 4
// 12
// Ready to packThe quote marks tell JavaScript where the text starts and ends; they are not part of the printed message. Matching single quotes and matching double quotes both work. Use one pair around the whole message. Curly “smart quotes” copied from a document are not JavaScript string delimiters.
Removing quotes does not mean “print these words without quotes.” It asks JavaScript to interpret those words as code. In the following program, no value has been given the name notebook, so looking it up fails.
console.log("About to inspect a name");
console.log(notebook);
console.log("This statement is not reached");
// ✅ Expected output:
// About to inspect a name
// ✅ Expected error:
// ReferenceError: notebook is not definedRead the error category and the name it mentions before changing anything. This ReferenceError means a name could not be resolved. If you intended literal text, add quotes. If you intended stored information, the name must be declared first; Variables and Data Types owns that operation.
A silent expression is different from a broken program
3 * 4;
console.log("Only this message is printed");
console.log(3 * 4);
// ✅ Expected output:
// Only this message is printed
// 12A calculation can succeed without producing console output. Our editor runs the entire program and shows its console messages. A browser developer console may additionally display the value of an expression you enter directly; do not rely on that extra display when writing a script.
Each press of Run starts a fresh example page in this editor. Code from another example is not a hidden prerequisite. If you need a value, put its declaration in the program you are running. Reset restores the example; Back to Lesson returns to the explanation.
Find the first failing statement
Names are case-sensitive. console.log and console.Log ask for different properties. The console provides the lowercase method; a capital letter here is a bug, not a formatting preference.
console.log("Packing started");
console.Log("Packing finished");
console.log("Label printed");
// ✅ Expected output:
// Packing started
// ✅ Expected error:
// TypeError: console.Log is not a functionNotice what was printed before the error. Execution reached the first statement, then failed at the method call. Repair that call and rerun the whole example. There is no need to change the message or the arithmetic.
A syntax error happens earlier: JavaScript cannot finish parsing the program. In this broken example the closing parenthesis is missing. Even the valid statement before it does not run, because the program has not been accepted for execution.
console.log("Starting");
console.log("Packing";
// ✅ Expected error:
// SyntaxError: missing ) after argument listThe checked error wording comes from the runtimes used for these examples. A different browser may phrase the same error differently. The useful diagnosis is the category, the offending code and whether execution had started. For these straight-line programs, successful statements run in order; callbacks and asynchronous work have their own scheduling rules.
Leave an explanation without adding output
// The packing allowance is two extra labels.
console.log(5 + 2);
/* This note explains the program.
It is not a message for the customer. */
console.log("Labels prepared");
// ✅ Expected output:
// 7
// Labels preparedA // comment continues to the end of its line. A block comment runs from /* to */. Comments explain intent to a person reading the source; they are not instructions that print themselves. Prefer a note about why two labels are added over a note that merely repeats “add two.”
Complete the packing slip
Fill the two blanks: the first is a quoted message, and the second is the quantity calculation. Keep the surrounding parentheses and semicolons. The panel describes the program after both blanks are filled.
console.log(___); // 👉 replace ___ with "Packing slip"
console.log(___); // 👉 replace ___ with 4 * 3
console.log("Labels included");
// ✅ Expected output:
// Packing slip
// 12
// Labels includedIf the editor says a blank remains, search for ___ in executable code. The hint comments can remain. If a word becomes a ReferenceError instead, check whether you replaced a quoted string with an unquoted name.
Write a receipt another person can read
Write three console statements for a stationery order. The first prints Pen order. The second calculates the cost of six pens at two units each. The third prints Collect at desk. Add one comment explaining the calculation. Use no variables yet: the task is to separate messages from expressions and make each result visible.
console.log("Pen order");
// Six pens, each costing two units.
console.log(6 * 2);
console.log("Collect at desk");
// ✅ Expected output:
// Pen order
// 12
// Collect at deskAfter it works, change the price in the expression. Check that the numeric line changes while both messages remain the same. This edit–run–compare loop is the habit to keep when programs get longer.
For the language and browser distinction, see MDN’s JavaScript introduction. You do not need a server project for the console examples on this page.
Practice quiz
What does this complete program print? console.log("Pack"); console.log("Send");
- Pack Send
- Send Pack
- Pack Send
- Nothing is printed
Answer: Pack Send. Each call prints one line, and these two statements execute in source order.
What does this complete program print? console.log("4 + 2");
- 6
- 4 + 2
- 42
- ReferenceError: four is not defined
Answer: 4 + 2. The quoted characters are text. JavaScript does not evaluate arithmetic inside ordinary quotes.
What does this complete program print? console.log(7 * 3);
- 73
- 10
- 21
- 7 * 3
Answer: 21. The multiplication is evaluated before console.log receives its argument.
What does this complete program print? 8 * 2; console.log("Done");
- 16 Done
- 16
- Nothing is printed
- Done
Answer: Done. The first expression is evaluated but not logged. The explicit console call prints the message.
What does this complete program print? // console.log("Hidden"); console.log("Visible");
- Visible
- Hidden Visible
- Hidden
- SyntaxError
Answer: Visible. The first line is a comment, so it does not execute.
Which error ends this program? console.Log("Hello");
- Hello
- TypeError: console.Log is not a function
- undefined
- ReferenceError: Hello is not defined
Answer: TypeError: console.Log is not a function. JavaScript property names are case-sensitive; the method is console.log.
A program contains a missing closing parenthesis. Why can a valid console.log above it produce no output?
- The console can print only numbers
- The browser starts at the last line
- The complete program must parse before its statements execute
- Comments erase previous output
Answer: The complete program must parse before its statements execute. A syntax error can prevent the whole program from starting. This differs from a runtime error after earlier statements have executed.
You log a message successfully, but the webpage heading has not changed. What does that show?
- JavaScript cannot change pages
- console.log is broken
- The message must use single quotes
- Console output and changing page elements are separate operations
Answer: Console output and changing page elements are separate operations. console.log reports values to the console. Updating a page requires a DOM operation.
Continue this course
- Next: Variables and Data Types — var, let, const — how to store and update data in your programs
- Quick reference: JavaScript cheat sheet