Syntax & Comments
JavaScript Syntax & Comments is a core JavaScript concept covering master the basic syntax rules of JavaScript, including variables, operators, This topic is essential for academic learning, board exam preparation, and developing optimized real-world code.
What are Comments?
Comments are notes written by the programmer to explain what the code is doing. They are ignored by the computer and do not affect how the program runs.
Why use Comments?
- Explain "Why": Don't explain what the code does (the code does that); explain why you wrote it that way.
- Organization: Use them to mark sections of a large file.
- Debugging: Temporarily "comment out" code to prevent it from running while testing.
- Collaboration: Help other developers understand your logic.
Write clear, concise comments. If your code is so complex it needs a paragraph of explanation, consider refactoring the code to be simpler!
JavaScript Syntax
JavaScript syntax is the set of rules that defines how JavaScript programs are constructed.
1. Identifiers (Names)
Identifiers are names for variables, functions, and keywords.
- Must begin with a letter,
$, or_. - Subsequent characters can be letters, digits, underscores, or dollar signs.
- Camel Case: By convention, JS uses
camelCase(e.g.,firstName,userScore).
2. Case Sensitivity
JavaScript is Case Sensitive. myVariable and myvariable are two different variables.
let lastName, lastname;
lastName = "Doe";
lastname = "Peterson";
3. Whitespace
JavaScript ignores multiple spaces. You can use whitespace to make your code more readable.
let x = 10;
let y=10; // Both are valid
Comments in JavaScript
Single-line Comments
Use //.
// This is a comment
let x = 5; // This is also a comment
Multi-line Comments
Start with /* and end with */.
/*
This code will
print multiple lines
of text.
*/
In JavaScript, semicolons are used to separate statements. While often optional (the browser will guess where they belong), it is a professional standard to always include them to prevent rare but confusing bugs.