Lesson 5 of 30 – JavaScript Syntax
17%

JavaScript Syntax

JavaScript syntax is a set of rules that define how JavaScript programs are written and interpreted by the browser.

Note: JavaScript code can be written inside the HTML <script> tag or in a separate file with the .js extension.
Writing JavaScript Code

JavaScript code is generally written inside the HTML script tag.

<script>
// Write JavaScript code here
</script>

You can also save JavaScript code in an external file with the .js extension.

Character Set

JavaScript uses the Unicode character set.

  • Supports letters.
  • Supports numbers.
  • Supports symbols.
  • Supports punctuation marks.
  • Supports many international languages.
Case Sensitive Language

JavaScript is a case-sensitive programming language.

Word 1 Word 2 Same?
name Name No
myFunction MyFunction No
age Age No

Always use the correct capitalization while writing JavaScript code.

Variables

Variables are used to store data in JavaScript.

Example:
<script>
var name = "Steve";
var id = 10;
</script>

A variable can store text, numbers, and other types of data.

JavaScript Statements

A JavaScript program consists of one or more statements. Statements tell the browser what action to perform.

Example:
<script>
var a = 10;
var b = 20;
var sum = a + b;
</script>

Each line above is a JavaScript statement.

Semicolons

Semicolons separate JavaScript statements. They are optional but recommended.

Example:
var one = 1;
var two = 2;
var three = 3;

var four = 4
var five = 5

Both styles work, but using semicolons improves readability.

Whitespaces

JavaScript ignores extra spaces and tabs.

The following statements are equivalent:
var one=1;

var one = 1;

var one = 1;

Use spaces to make your code easier to read.

Comments

Comments explain the code and are ignored during execution.

Single-line Comment:
// This is a comment
Multi-line Comment:
/*
This is a
multi-line comment
*/
Example:
var one = 1; // Single line comment

/* Multi-line
comment */

var two = 2;
Strings

A string is text enclosed in quotation marks.

Double Quotes:
var msg = "Hello World";
Single Quotes:
var msg = 'Hello World';

Both methods are valid in JavaScript.

Numbers and Booleans
Numbers

Numbers should not be enclosed in quotation marks.

var num = 100;
var price = 10.5;

Booleans

Boolean values can only be true or false.

var yes = true;
var no = false;
JavaScript Keywords

Keywords are reserved words that have special meanings in JavaScript. They cannot be used as variable names or function names.

Keyword Keyword Keyword
var function if
else do while
for switch break
continue return try
catch finally debugger
case class this
default true false
in instanceof typeof
new null throw
void with delete
Syntax Rules Summary
  • JavaScript is case-sensitive.
  • Variables store data.
  • Statements perform actions.
  • Semicolons are recommended.
  • Extra spaces are ignored.
  • Comments improve readability.
  • Strings use single or double quotes.
  • Numbers are written without quotes.
  • Boolean values are true or false.
  • Keywords are reserved words.
Best Practices
  • Use meaningful variable names.
  • Write clean and readable code.
  • Indent code properly.
  • Use comments when necessary.
  • Always check case sensitivity.
  • Use semicolons consistently.
  • Avoid using reserved keywords as identifiers.

🧠 Quick Quiz

Is JavaScript a case-sensitive language?