JavaScript Tutorial

JavaScript Syntax

Identifiers, literals, and operators. JavaScript is case-sensitive: Name and name are different.

The pieces of a line

A typical line has a name, an operator, and a value. The name is an identifier. The written value is a literal. The operator says what to do.

Example

let total = 10 + 2;
console.log(total);

total is the identifier. 10 and 2 are number literals. +and = are operators. let is a keyword.

Try the examples with Try it in JavaScript. That opens /javascript/try.

Identifiers

An identifier is a name you choose: a variable, a function, or a parameter. Start with a letter, underscore, or $. After that you may use letters, digits, underscores, or $. Do not start with a digit. Do not put a space or a hyphen in the name.

ValidInvalid
count, player2, _tmp, $el2player, my-score, my score

Choose names that say what the value is. score is better than x unless you are in a tiny math example.

Literals

A literal is a value written directly in the code. You do not look it up. You write it.

Example

console.log(42);
console.log(3.14);
console.log("Ada");
console.log(true);
console.log(null);
KindExample
Number42, 3.14
String"Ada", 'Ada'
Booleantrue, false
Nothingnull, undefined

JavaScript is case-sensitive

Name and name are different identifiers. Console.log is notconsole.log. Keywords must be lowercase: Let is not let.

Example

let name = "Ada";
let Name = "Grace";
console.log(name);
console.log(Name);

The console prints two strings. They are two boxes. Pick one spelling and keep it.

Reserved words

Some words already belong to the language. You cannot use them as variable names. The engine will report a syntax error.

KeywordRole
let, const, varDeclare a name
if, else, switchChoose a branch
function, returnDefine and leave a function
true, false, nullBuilt-in values

If a name is already a keyword, pick another word: className instead of class,value instead of default.

Operators

Operators combine or compare values. Arithmetic uses +, -, *,/, and %. Assignment uses =. Comparison uses === and!==. Later chapters cover each group in full.

Example

let n = 7;
console.log(n + 3);
console.log(n === 7);
console.log(n !== 0);

Strings are not identifiers

Quotes make a string. No quotes make a name. "score" is text. score is a variable. Mixing them up is a common first error: you log the word instead of the value, or you use a name that was never declared.

Next: comments, so you can leave notes that the engine skips.

FAQ: JavaScript Syntax

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript syntax examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript syntax in this JavaScript JavaScript lesson (JavaScript Syntax).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.