JavaScript Basic Syntax
JavaScript syntax is the set of rules and structures that must be followed when writing JavaScript code. Mastering the basic syntax is the first and most important step in learning JavaScript. In this chapter, we'll learn the basic syntax rules of JavaScript.
JavaScript Statements
JavaScript programs consist of a series of executable statements. Statements are instructions that perform specific tasks. In JavaScript, statements usually end with a semicolon (;), but the semicolon is optional.
Statement Separators
Although semicolons are optional, it's recommended to always use semicolons to separate statements for code clarity and to avoid potential issues.
JavaScript Code Blocks
JavaScript statements can be combined into code blocks, wrapped with curly braces {}. Code blocks are typically used in functions, conditional statements, and loop statements.
JavaScript Comments
Comments are parts of the code that are not executed, used to explain the purpose of the code and improve readability.
Single-Line Comments
Comments starting with // are single-line comments, where the comment content extends from // to the end of the line.
Multi-Line Comments
Comments wrapped with /* */ are multi-line comments that can span multiple lines.
JavaScript Variables
Variables are containers for storing data values. In JavaScript, variables are declared using the var, let, or const keywords.
Variable Naming Rules
- Variable names must start with a letter, underscore (_), or dollar sign ($)
- Variable names can contain letters, numbers, underscores, and dollar signs
- Variable names are case-sensitive
- JavaScript reserved keywords cannot be used as variable names
JavaScript Data Types
JavaScript has multiple data types, including:
- String: Used to store text
- Number: Used to store numeric values
- Boolean: Represents true or false
- Object: Used to store complex data
- Array: Special object type used to store ordered data collections
- null: Represents "no value"
- undefined: Represents an undefined value
JavaScript Operators
Operators are used to perform operations on operands.
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
JavaScript Functions
Functions are reusable code blocks used to perform specific tasks.
JavaScript Conditional Statements
Conditional statements are used to execute different code blocks based on different conditions.
JavaScript Loop Statements
Loop statements are used to repeatedly execute code blocks.
JavaScript Error Handling
JavaScript provides the try...catch statement to handle errors.
JavaScript Strict Mode
Strict mode is a restricted variant of JavaScript that eliminates some of the unreasonable and imprecise aspects of the JavaScript language.
JavaScript Code Style Suggestions
- Use consistent indentation: Usually use 2 or 4 spaces for indentation
- Use meaningful variable names: Variable names should clearly express their purpose
- Add comments: Add comments to explain complex logic
- Keep code concise: Avoid overly long lines of code and deeply nested structures
- Use code formatting tools: Use tools like Prettier to maintain consistent code style
Summary
JavaScript basic syntax includes:
- Statements: The basic unit of JavaScript programs, usually ending with a semicolon
- Code Blocks: A collection of statements wrapped in curly braces
- Comments: Single-line comments (//) and multi-line comments (/* */)
- Variables: Declared using var, let, const, following naming rules
- Data Types: Strings, numbers, booleans, objects, arrays, etc.
- Operators: Arithmetic, assignment, comparison, logical operators
- Functions: Reusable code blocks
- Conditional Statements: if...else statements
- Loop Statements: for loops, while loops
- Error Handling: try...catch statements
- Strict Mode: Improves code quality and safety
Mastering these basic syntax elements is an important first step in learning JavaScript. In the next chapter, we'll dive deeper into JavaScript data types.