Getting Started
Syntax and Structure
JavaScript syntax defines the set of rules for writing valid JavaScript code. It consists of statements, expressions, operators, and control structures that determine how the script is executed.
JavaScript Statements
A JavaScript program consists of a sequence of statements. Each statement performs an action and is typically written on a new line.
Example:
JavaScript statements can be terminated with a semicolon (;
), though it is optional in most cases.
Comments in JavaScript
Comments are used to document code and prevent execution of specific lines.
Single-line comment:
Multi-line comment:
Variables and Constants
Variables store data values and can be declared using var
, let
, or const
.
Declaring Variables
let
– Block-scoped variable (recommended for most use cases).var
– Function-scoped variable (older syntax, avoid using it in modern development).const
– Block-scoped, immutable variable (cannot be reassigned).
Variable Naming Rules
Must begin with a letter, underscore (
_
), or dollar sign ($
).Cannot use reserved JavaScript keywords.
Case-sensitive (
myVariable
andMyVariable
are different).
Data Types in JavaScript
JavaScript is dynamically typed, meaning variables do not have fixed types.
Primitive Data Types
JavaScript has six primitive types:
String –
"Hello"
Number –
42
,3.14
Boolean –
true
,false
Undefined – A variable that has been declared but not assigned a value
Null – Represents an intentional absence of value
Symbol – Unique and immutable identifier (used in advanced programming)
Example:
Non-Primitive (Reference) Data Types
Objects – Collections of key-value pairs.
Arrays – Ordered lists of values.
Functions – Blocks of reusable code.
Example:
Operators in JavaScript
JavaScript includes several types of operators:
Arithmetic Operators
Used for mathematical operations.
Comparison Operators
Used for value comparisons.
Logical Operators
Used to combine multiple conditions.
Conclusion
This page introduced JavaScript syntax, variables, data types, and operators. In the next section, we will explore control flow structures like conditionals and loops to build more complex logic.
Join our Community Forum
Any other questions? Get in touch