- You should follow these rules to maintain your code neat and bug-free.
Syntax and Case Sensitivity
var a = 7; var b = 5; var c = a + b;
To begin with, the first character of an identifier has to be either a letter, a dollar sign (
$), or an underscore (
_). So, for example,
Note: a number cannot be the first character of an identifier.
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 (
Types of Literals
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:
var name1 = "value1"; var name2 = 'value2';
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
false. Check out the example below:
var xyz = true; var xyz = false;
Variables and Operators
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:
var a; a = 10;
You should use the assignment operator (
var a = 20;
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:
(7 + 5) * 12
2 * 2 will evaluate to
4, like in the example below:
var a = 2 * 2;
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.
a * 8
The values can also be both variables and strings combined. For example, the expression
"Joe" + " " + "Johnson", will evaluate to
"Joe" + " " + "Johnson"
Keywords and Comments
var tells the browser to create a new variable:
var a = 7 + 5; var b = a * 12;
// or in between
*/ is referred to as a comment. This function is similar to HTML Comments.
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