By WebNest Studio Engineering Team
JavaScript Tutorial
JavaScript Syntax
JavaScript syntax is the set of rules that determine how a valid JavaScript program is written and structured. Unlike languages such as Java or C#, JavaScript does not require a surrounding class or explicit entry point — a script is simply a sequence of statements that the JavaScript engine reads and executes from top to bottom.
Because JavaScript is loosely structured and forgiving in places (like optional semicolons), it is especially important to understand exactly what the engine does with your code, rather than relying on "it seems to work." Small syntax habits formed early — statement structure, semicolon usage, and commenting — carry through every other topic in the language.
Why it matters
JavaScript inserts missing semicolons for you, and in one situation it inserts one where you did not want it: directly after return. The code is valid, runs without a syntax error and silently returns undefined. Knowing the basic rules of statements and semicolons saves you from this class of quiet bug.
Statements and Expressions
A JavaScript program is made of statements — instructions that the engine executes, such as declaring a variable, running a loop, or calling a function. An expression is anything that produces a value, such as 2 + 2 or "hello".toUpperCase(). Statements are often built from expressions: let total = price * quantity; is a statement containing the expression price * quantity.
- Statements end a "step" of execution: declarations, assignments, conditionals, loops, function calls.
- Expressions always evaluate to a value and can be nested inside other expressions or statements.
- Blocks of statements are grouped with curly braces
{ }, used by functions, loops, and conditionals.
Semicolons and Automatic Semicolon Insertion (ASI)
JavaScript statements are conventionally ended with a semicolon, but the language includes a feature called Automatic Semicolon Insertion (ASI), which inserts semicolons for you in certain cases when you omit them. ASI is convenient but has sharp edges: it does not always insert a semicolon where you expect, particularly with return statements followed by a newline, or lines starting with ( or [ that can be misread as a continuation of the previous line. The safest professional practice is to write semicolons explicitly rather than rely on ASI.
Comments
JavaScript supports single-line comments starting with //, which run to the end of the line, and multi-line comments wrapped in /* ... */, which can span several lines. Comments are ignored entirely by the engine and exist only to communicate intent to human readers.
Case Sensitivity and Whitespace
JavaScript is case-sensitive, so myValue and myvalue are different identifiers. Whitespace (spaces, tabs, newlines) is generally insignificant between tokens and is used only for readability, except inside string literals where it is preserved exactly as written.
Examples
Statements, expressions, and a semicolon-omission pitfall caused by ASI
function getValue() {
return
{
value: 42
};
}
console.log(getValue()); // ASI inserts a semicolon right after "return"
function getValueFixed() {
return {
value: 42
};
}
console.log(getValueFixed());
undefined
{ value: 42 }
Single-line and multi-line comments alongside real statements
// Calculate the total price for an order
const price = 25;
const quantity = 3;
/*
Multiplying price by quantity gives the subtotal.
Tax is added separately in a later step.
*/
const subtotal = price * quantity;
console.log(subtotal);
75
Common Mistakes
- Putting a newline right after "return" when returning an object literal — ASI inserts a semicolon after "return", silently turning the function into one that returns undefined.
- Assuming semicolons are never needed because "JavaScript works fine without them" — ASI has documented edge cases that produce bugs which are hard to spot.
- Mixing tabs and spaces inconsistently, which does not break JavaScript syntax but makes code much harder to read and review.
- Forgetting that JavaScript is case-sensitive and accidentally declaring two different variables (e.g. "userName" and "username") instead of reusing one.
Exercise
The function is meant to return an object, but because the object starts on the line after return, JavaScript ends the statement there and the function returns undefined. Fix it.
Expected output: Asha
function getUser() {
return
{
name: "Asha"
};
}
console.log(getUser().name);
Show hints
- A line break directly after
returnends the statement. - Start the object on the same line:
return {.
Show solution
function getUser() {
return {
name: "Asha"
};
}
console.log(getUser().name);
Quick quiz
1. Is JavaScript case-sensitive?
- A. No
- B. Yes; total and Total are different names
- C. Only in strict mode
- D. Only for keywords
Show answer
B. Variable names, function names and keywords are all case-sensitive.
2. How is a single-line comment written?
- A. <!-- comment -->
- B. # comment
- C. // comment
- D. -- comment
Show answer
C. A multi-line comment is written between /* and */.
3. What does automatic semicolon insertion do?
- A. Removes semicolons from the code
- B. Reports an error for every missing semicolon
- C. Formats the code
- D. Adds semicolons where the parser decides a statement has ended
Show answer
D. It usually does what you intend, with a few traps such as a line break after return.
Interview questions
What is the difference between a statement and an expression?
Show answer
An expression produces a value: 2 + 3, user.name, a function call. A statement performs an action: a variable declaration, an if, a loop, a return. An expression can be used wherever a value is expected; a statement cannot. This is why the conditional operator, which is an expression, can appear inside a template literal or an argument list, while an if statement cannot.
Should you write semicolons in JavaScript?
Show answer
They are optional in most places because of automatic semicolon insertion, but the rules have exceptions: a line starting with (, [ or a template literal is joined to the previous line, and a line break after return ends the statement. Either style works if applied consistently. Most teams settle the question with a formatter such as Prettier, which adds or removes them automatically.
Key Points to Remember
- A JavaScript program is a sequence of statements built from expressions, executed top to bottom.
- Automatic Semicolon Insertion (ASI) can insert semicolons in unexpected places — write semicolons explicitly to avoid subtle bugs.
- JavaScript supports // single-line and /* ... */ multi-line comments, both ignored by the engine.
- JavaScript is case-sensitive; whitespace is insignificant outside of string literals.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering