Like any other programming language, JavaScript has a set of specific rules for writing code. These rules are called JavaScript syntax. Writing JavaScript code neatly and syntactically correct makes your code bug-free and easy to read.
In this JavaScript syntax tutorial, you'll find the rules for writing JavaScript code. You will learn correct capitalization of function names, variables, and other identifiers. You'll also find examples which will show you how JavaScript code should be written.
Contents
JavaScript Syntax: Main Tips
- This is a collection of specific rules on how programs in JavaScript are constructed.
- You should follow these rules to maintain your code neat and bug-free.
- JavaScript statements may include any of these things: operators, values, keywords, expressions, and comments.
Syntax and Case Sensitivity
By definition, a computer program is like a list of instructions for the computer to execute. We refer to these instructions for the computer as statements. However, JavaScript gives commands to the web browser instead of the computer itself.
JavaScript statements don't need to be placed in parentheses or braces. They are, however, separated by semicolons:
In JavaScript, identifiers are used to name variables, keywords, functions, and labels. Their naming rules are not much different from any other programming language.
To begin with, the first character of an identifier has to be either a letter, a dollar sign ($
), or an underscore (_
). So, for example, A
, _
, or $
in JavaScript are all identifiers. The following characters can be digits, letters, underscores, or dollar signs.
Note: a number cannot be the first character of an identifier.
Each JavaScript identifier is case sensitive: for example, Name
and name
, are not the same variable in JavaScript:
Name = "Joe";
name = "John"; // Now we have two separate string variables
Note: same rules apply to keywords and other identifiers as well.
Whenever case sensitivity is relevant, it's important to have a clear standard of writing names that consist of more than one word. Throughout the history of programming, there have been three most popular methods to achieve that: hyphens (first-name
), underscores (first_name
), and camel case (FirstName
).
Camel case is the default identifier writing method in JavaScript and many other programming languages. Keep in mind though that in JavaScript the identifier name usually begins with a lowercase letter (firstName
)
Note: the hyphens method does not work in JavaScript. Hyphens are only used for substractions.
Types of Literals
Literals define fixed values used in JavaScript. There are a few types of literals we use, and they have their own rules to be followed.
Number literals, according to JavaScript syntax, can be written with or without the decimal point. This is shown in the example below:
Note: Be sure to use a decimal point (
.
) and not a comma (,
).
Another type of a literal is a string. It defines a piece of text within quotes. String values can be written in single or double quotes:
Note: A string does not necessarily have to have a written value and can be an empty string instead.
Boolean literals have two values: either true
or false
. Check out the example below:
Variables and Operators
In programming languages, the purpose of JavaScript variables is to store data. Variables are written as var
in JavaScript syntax. So you need to use the var
keyword when declaring variables. You can assign values to variables using an equal sign =
.
In the example below, we declare a new variable a
and assign a value to it:
You should use the assignment operator (=
) for assigning values to JavaScript variables. The operators should be separated with spaces from both ends, as it is good coding practice and makes it easier to read:
When you need to define calculations, you can also use other arithmetic operators (+
, -
, *
, /
) for computing values. These operators must also be separated with spaces, as it makes the code easy to read:
Expressions
An expression in JavaScript is a combination of variables, values, and operators, that is computed to a value. We refer to this computation as an evaluation. Therefore, it becomes an expression. For example, the expression 2 * 2
will evaluate to 4
, like in the example below:
Some expressions can include variables as well. However, this is not mandatory - you don't have to include them if you don't need them.
The values can also be both variables and strings combined. For example, the expression "Joe" + " " + "Johnson"
, will evaluate to "Joe Johnson"
:
Keywords and Comments
JavaScript keywords are used to identify actions to be performed by JavaScript. JavaScript keywords also help the browser to establish what action to perform. For example, the most popular keyword var
tells the browser to create a new variable:
Not every JavaScript statement is meant to be executed by the browser. The code written after double slashes //
or in between /*
and */
is referred to as a comment. This function is similar to HTML Comments.
Comments are used by programmers to leave notes for themselves and other developers to improve the readability of the code. It is especially useful in large JavaScript files, where it is easy to get lost between the statements. Also, it is helpful when other developers read your code.
Comments are ignored by the browser and not displayed:
var a = 7; // This statement is going to be executed
// var b = 5; This statement is not going to be executed. It is just a comment
JavaScript Syntax: Summary
- JavaScript syntax is rather strict.
- JavaScript operators must be separated with spaces from both ends.
- Letter,
_
, or$
in JavaScript are identifiers. Numbers cannot be identifiers. - JavaScript is case sensitive. Mixed letter size can cause bugs.