JavaScript is a powerful and versatile programming language used widely in web development. A crucial aspect of mastering JavaScript is understanding its lexical structure. This blog post delves into the foundational elements that form the building blocks of writing in JavaScript.
Learning
Case Sensitivity, Spaces, and Line Breaks
JavaScript is case-sensitive, meaning it differentiates between uppercase and lowercase letters. This rule applies to keywords, variables, function names, and other identifiers. Spaces and line breaks are generally ignored in JavaScript, which allows for flexible formatting and indentation in coding.
Comments
JavaScript supports two types of comments: single-line and multi-line. Single-line comments start with //, and multi-line comments are enclosed between /* */
These are essential for making code readable and for explaining complex code segments.
Literals
Literals in JavaScript are fixed values directly written into the code, like
- Numbers - (12, 1.2)
- Strings - ("hello world", 'Hi'),
- Boolean - (true, false)
- Null.
Identifiers and Reserved Words
Identifiers are names given to elements like variables and functions. They must start with a letter, underscore (_), or dollar sign ($). JavaScript also has reserved words that cannot be used as identifiers, like if, while, and for.
Unicode
JavaScript uses Unicode character set, allowing for a wide range of characters in strings and comments. While ASCII characters are commonly used in identifiers, Unicode characters, including letters from non-English languages, are permissible.
Optional Semicolons
In JavaScript, semicolons are used to separate statements but can often be omitted. The language has specific rules for when semicolons can be skipped, such as when statements are on separate lines or before a closing curly brace.
const test = 'Hello World'; <--- semicolon
Conclusion
The lexical structure of JavaScript forms the foundation of how we write and structure our code in this language. Understanding these basic rules – from case sensitivity to the use of semicolons – is crucial for any aspiring JavaScript developer. As we continue to explore JavaScript, these principles will become second nature, allowing us to write more efficient and error-free code.