JavaScript Syntax — Simple Beginner's Guide with Examples

JavaScript Syntax — Simple Beginner's Guide with Examples

JavaScript Syntax — Simple Beginner's Guide with Examples | TipsInLearning
Web Development

JavaScript Syntax

Learn JavaScript syntax in a simple way — understand statements, comments, variables, values, and operators with easy-to-follow examples.

10 min read
Beginner
Programming
Free Course
What You'll Learn
JavaScript syntax basics
How to write statements
Using comments
Variables and values
Operator types
Naming conventions

JavaScript Syntax Introduction

Syntax means the rules and structure of a programming language. JavaScript syntax tells you how to write code correctly so the browser understands it. Learning syntax is like learning the grammar of English — you need to follow the rules to communicate clearly.

Why syntax matters

Without proper syntax, your code won't work. The browser won't understand what you're trying to do and will show errors instead.


Statements

A statement is a single instruction that tells JavaScript to do something. Every statement usually ends with a semicolon ;

Statement examples

JavaScript — Statements
let name = "John";  // Statement 1
let age = 25;        // Statement 2
alert("Hello");      // Statement 3
Tip: Semicolons are not always required in JavaScript, but it's good practice to use them.

Comments

Comments explain your code. They don't execute, they just help you and others understand what the code does. There are two types of comments in JavaScript.

Single-line comments

Use // to write a comment on one line:

JavaScript — Single-line Comments
// This is a comment
let x = 10; // Variable x equals 10

Multi-line comments

Use /* */ for comments that span multiple lines:

JavaScript — Multi-line Comments
/* This is a multi-line comment
   It can span multiple lines
   Use it for longer explanations */
let age = 25;

Variables

A variable is like a labeled box that holds data. You create variables using let, const, or var.

Creating variables

JavaScript — Variables
// Using let (modern, recommended)
let name = "John";

// Using const (cannot be changed)
const PI = 3.14159;

// Using var (old, avoid in new code)
var age = 25;

Variable differences

KeywordCan ChangeBest For
letYesVariables that change
constNoValues that stay the same
varYesAvoid (old syntax)

Values

A value is the actual data you put into a variable. JavaScript values can be text, numbers, true/false, or more complex data.

Types of values

JavaScript — Values
let text = "Hello";        // String (text)
let number = 42;         // Number
let decimal = 3.14;       // Decimal number
let isTrue = true;       // Boolean
let empty = undefined;   // Empty value

Operators

Operators are symbols that perform actions on values. They let you do math, compare values, or combine text.

Common operator types

OperatorPurposeExample
+Addition5 + 3 = 8
-Subtraction10 - 4 = 6
*Multiplication6 * 2 = 12
/Division20 / 4 = 5
=Assign valuex = 10
==Equal to5 == 5

Operator examples

JavaScript — Operators
let x = 10;
let y = 5;

let sum = x + y;        // 15
let product = x * y;     // 50
let text = "Hello" + " World"; // "Hello World"

Case Sensitive

JavaScript is case sensitive, which means it treats uppercase and lowercase letters as different. Name and name are not the same.

Case sensitive examples

JavaScript — Case Sensitivity
let name = "John";   // Correct
let Name = "Jane";   // Different variable

alert(name);   // Shows "John"
alert(Name);   // Shows "Jane"

Variable Naming Rules

When naming variables, follow these rules to avoid errors and make your code readable:

Rules for naming

  • Start with a letter, underscore, or dollar sign — Never start with a number
  • Only use letters, numbers, underscores, and dollar signs — No spaces or special characters
  • Use camelCase for multiple words — myVariableName (not my_variable_name)
  • Use meaningful names — Use userName, not u or x
  • Avoid JavaScript keywords — Don't use let, function, if, etc. as variable names

Good vs bad naming

JavaScript — Good vs Bad Names
// Good variable names
let userName = "John";
let userAge = 25;
let isActive = true;

// Bad variable names (avoid these)
let u = "John";       // Too short
let user age = 25;   // Space in name (error!)
let 2fast = true;     // Starts with number

Best Practices

1
Use semicolons
End statements with semicolons for consistency
2
Use meaningful names
Make code readable with clear variable names
3
Add comments
Explain your code so others understand it
Syntax Summary
1Syntax is the rules for writing JavaScript correctly
2Statements end with semicolons and perform actions
3Comments explain code using // or /* */
4Variables store data using let, const, or var
5Operators perform math and comparisons (+, -, *, /)
6JavaScript is case sensitive and has naming rules

Frequently Asked Questions

JavaScript Syntax is the set of rules for writing JavaScript code. It includes how to write statements, comments, variables, and operators correctly.

A statement is a single instruction that tells JavaScript to do something. Statements usually end with a semicolon.

Comments explain your code. Single-line comments use // and multi-line comments use /* */. Comments don't execute.

let allows variables to change, while const creates variables that cannot be changed after creation. Use const by default, let when you need to change values.

Yes, JavaScript is case sensitive. "Name" and "name" are different variables. Always match the exact case.

Use meaningful names in camelCase (userName, userAge). Start with a letter, underscore, or dollar sign. Never start with numbers.

Post a Comment