JavaScript: Core Language Features
1. Lexical Structure
The lexical structure defines the basic syntax and rules for writing JavaScript code. It
includes how code is written and structured at the lowest level.
- Case Sensitivity: JavaScript is case-sensitive. For example, 'Variable' and 'variable'
are treated as different.
- Whitespace and Line Breaks: These are mostly ignored except to separate tokens.
Use indentation for readability.
- Comments:
- Single-line comment: // This is a single-line comment
- Multi-line comment:
/*
This is a multi-line
comment
*/
- Semicolons: Used to terminate statements. JavaScript can insert them
automatically, but it is good practice to include them.
- Identifiers: Names for variables, functions, etc. They must begin with a letter,
underscore (_), or dollar sign ($), and cannot be reserved words.
2. Variables and Identifiers
Variables are containers for storing data values.
You can declare variables using var, let, or const:
- var: Function-scoped and hoisted. Avoid using in modern code.
- let: Block-scoped and supports re-assignment.
- const: Block-scoped and must be assigned at declaration. Cannot be reassigned.
Example:
let age = 25;
const name = "John";
var score = 90;
Identifiers are names for variables and functions. They must not be JavaScript
reserved keywords and must follow naming rules.
3. Data Types and Values
JavaScript supports different data types, divided into primitive and non-primitive
types.
Primitive Types:
- Number: Any numeric value (e.g., 5, 3.14)
- String: Sequence of characters (e.g., "Hello")
- Boolean: true or false
- Undefined: A declared variable without a value
- Null: Represents intentional absence of value
- BigInt: For very large integers
- Symbol: Unique and immutable identifier
Non-Primitive (Reference) Types:
- Object: Collection of properties (e.g., {name: "Ali"})
- Array: Ordered list of values (e.g., [1, 2, 3])
- Function: Callable blocks of code
4. Scope
Scope determines the accessibility of variables.
- Global Scope: Variables declared outside functions are global and accessible
everywhere.
- Function Scope: Variables declared with var inside a function are function-scoped.
- Block Scope: Variables declared with let and const inside curly braces ({}) are
block-scoped.
Example:
function greet() {
let message = "Hello";
[Link](message);
5. Literals
Literals represent fixed values in JavaScript code.
- Numeric Literal: 100, 3.14
- String Literal: "Hello", 'World'
- Boolean Literal: true, false
- Array Literal: [1, 2, 3]
- Object Literal: {name: "Ali", age: 20}
- Null Literal: null
6. Reserved Words
Reserved words are predefined by JavaScript and cannot be used as identifiers.
Examples include:
break, case, catch, class, const, continue, debugger,
default, delete, do, else, export, false, finally, for,
function, if, import, in, instanceof, let, new, null,
return, super, switch, this, throw, true, try, typeof,
var, void, while, with, yield
7. Expressions and Operators
Expressions are combinations of values and operators that compute a value.
Types of Operators:
- Arithmetic: +, -, *, /, %, **
- Assignment: =, +=, -=, *=, /=
- Comparison: ==, ===, !=, !==, >, <, >=, <=
- Logical: &&, ||, !
- Unary: typeof, delete, ++, --
- Ternary: condition ? expr1 : expr2
Example:
let result = (5 > 3) ? "Yes" : "No";
8. Statements
Statements are instructions that perform actions. Each statement ends with a
semicolon.
Types of Statements:
- Declaration: let x = 10;
- Conditional:
if (x > 0) {
[Link]("Positive");
} else {
[Link]("Negative");
- Loops:
for (let i = 0; i < 5; i++) {
[Link](i);
- Function Declaration:
function greet() {
[Link]("Hello");
- Control Flow: break, continue, return