Understanding JavaScript Syntax
JavaScript Syntax Syntax Guidelines JavaScript syntax consists of the rules for constructing programs: JavaScript Values In JavaScript, values are categorized into two types: Li...
JavaScript Syntax
Syntax Guidelines
JavaScript syntax consists of the rules for constructing programs:
// Variable Declaration:
let x = 5;
let y = 6;
// Value Computation:
let z = x + y;
// This is a comment, which has no effect
JavaScript Values
In JavaScript, values are categorized into two types:
- Literals: These are fixed values.
- Variables: These are values that can change.
JavaScript Literals
The key syntax rules for literals include:
- Numbers: Can be written with or without decimals.
- Strings: Text values enclosed in either double or single quotes.
JavaScript Keywords
JavaScript includes specific keywords to indicate actions. For example, let and const are used for declaring variables. Keep in mind, keywords are case-sensitive, so let is different from LET or Let.
JavaScript Variables
Variables serve as containers for storing data values and must have unique names for identification.
JavaScript Identifiers
An identifier is a name assigned to a variable. Rules for identifiers are:
- Start with a letter, underscore (_), or dollar sign ($).
- May include digits after the first character.
- Cannot be a reserved keyword (e.g., let, const, if).
- Are case-sensitive.
JavaScript Operators
JavaScript uses the assignment operator (=) to set variable values and arithmetic operators (+, -, *, /) to compute values.
JavaScript Expressions
An expression combines values, variables, and operators to yield a result.
JavaScript is Case Sensitive
Identifiers in JavaScript are case-sensitive. Therefore, lastName and lastname refer to separate variables.
JavaScript and Camel Case
Variable names in JavaScript are often formatted using lower camel case. Here are some conventions:
- Hyphens: Not allowed as they are reserved for subtraction.
- Underscore: e.g., first_name, last_name.
- Upper Camel Case (Pascal Case): e.g., FirstName, LastName.
- Lower Camel Case: e.g., firstName, lastName. This is commonly used in JavaScript.