You must understand that learning JavaScript or other programming languages is like learning a new language in the real world. There are rules guiding how you use the alphabet, words, and symbols. And you must get used to these rules if you want to be fluent in the language.
And while you learn javascript, keep this in mind as well. If you can comprehend JS statements and syntax, you can have a good time coding with JavaScript, which dominates the community of web development. So what are JavaScript statements and syntax?
Dive in; let’s get started.
Understanding JS Statements
A list of instructions you write adhering to some rules is what JS statements entail. So, a well-written JavaScript program is a JS statement, right?
A comprehensive JavaScript statement embeds the following:
Operators(= - + < > /)
Values ( 2, 5.7, “hello”)l
Comments(//, /* */)
Keywords(if, let, const)
JavaScript statements are executed in order. This means that the compiler runs the code in line 1 before moving on to lines 2 and 3.
When it encounters a statement that deviates from the JavaScript rules (syntax), it breaks and issues a warning. So, what is JavaScript syntax?
What is JS Syntax?
JS syntax refers to the rules you must follow to make a JavaScript statement optimized and runnable by the JavaScript compiler. Just as you would adhere to a certain pattern when creating an English sentence, which demands a decent placement of punctuation signs and the use of nouns, verbs, and adverbs.
In the same way, there are lists of elements you must place properly to make a concise JavaScript statement. Let’s look at some of the rules you must follow when writing a program with JavaScript.
Guidelines on How to make a Concise JavaScript Statement
Every programming language has a rule it follows; JavaScript is not exempted. Some of the rules for writing a simple JavaScript program are;
1. Use of semicolons
The first thing you must acknowledge is the usage of semicolons immediately after an executable statement or assignment.
Let’s use this example to illustrate the above description.
// this line of code initializes x,y,z,sum
let x, y, z, sum;
// let’s assign values to the variables
x = 35;
y = 45;
z = 55;
// we add up the values of x,y,z and store them in sum
sum = x + y + z;
Take note of how each of the expressions ends with a semicolon. JavaScript recognizes the end of a statement when it comes across a semicolon. This indicates that the code mentioned above can be written on a single line.
x = 35; y = 45; z = 55; sum = x + y + z;
2. White Spaces
Again, since JavaScript does not frown at white spaces, unlike other programming languages like Python, it is a good practice to include white spaces for best readability.
White space is the space you give after inputting a single character or variable.
3. Line Breaks
This is another best practice when writing programs with JavaScript, but you must ensure you only break after an operator.
For example,
document.getElementByClassName("regNum") =
" 203463783UIP"
Notice in the example that I entered another line in continuation of the previous line after the “=” operator.
4. Variable Naming
When giving your variables names, it is important to avoid initializing a variable with a JavaScript keyword. A keyword in JavaScript refers to some special reserved words you cannot use as variable names, function names, or labels.
Some of them are:
let | char | catch | for | with |
break | null | continue | import | yield |
if | delete | while | float | static |
else | const | function | double | int |
var | do | new | switch | class |
5. JavaScript Identifiers and Naming Structure
Identifiers in JavaScript are the characters or symbols you combine when initializing variables. They are also used to name functions and keywords.
To avoid errors, ensure you do the following when assigning names to variables.
1, Ensure names come from the letters in the alphabet, either in upper or lower case(A-Z or a-z
2, You can begin a Name with a dollar sign($).
3, Never begin a variable’s name with a number.
4, You can use an underscore(_) to separate two words if that’s your preferred naming choice.
5, Hyphens(-) are prohibited in JavaScript.
6, JavaScript is case-sensitive, which means two words using different cases are not the same. For example, lastName and lastname are not similar. You can name variables using the upper camel case (First Name, Surname), underscore (first_name, file_name), and lower camel case (javaScript, lastName).
Conclusion
In this tutorial, we talked about the JavaScript naming structure and how to ensure you make error-free statements. In our next tutorial, we will look at comments and variables. Until then, stay safe.