JavaScript Syntax
JavaScript syntax is the set of rules, how JavaScript programs are constructed:
var x;
let y;
// How to use variables:
x = 5;
y = 6;
let z = x + y;
JavaScript Values
The way JavaScript is written talks about two different kinds of things.:
- Fixed values
- Variable values
Numbers that don't change are known as literals
.
Variable values are referred to as Variables
.
JavaScript Literals
The top two essential rules for fixed values are:
1. Numbers
can be written with or without decimal points.
2. Strings
are words or sentences enclosed in either double or single quotation marks:
JavaScript Variables
In a programming code, variables are like containers used to hold data..
JavaScript uses special words like var
, let
and const
to create and name variables.
A "equal sign" is used to give values to variables.
In this example, we have a variable called "x." We're saying that "x" is given the value of 6.
JavaScript Operators
JavaScript uses math symbols like plus (+), minus (-), multiply (*), and divide (/) to calculate values.
In HTML, JavaScript uses something called an assignment operator
, which is represented by the equal sign (=
). Its job is to put values into variables.
JavaScript Expressions
An expression
is when you put numbers, letters, and symbols together to get a result.
The calculation is referred to as an assessment.
For instance, when you multiply 5 by 10, you get the result of 50:
Expressions can also contain variable values:
The values can come in different forms, like numbers and words.
For example, "John" + " " + "Doe", evaluates to "John Doe":
JavaScript Keywords
JavaScript keywords are words that tell the computer what to do.
The let
keyword in HTML instructs the web browser to make variables.
The word var
tells the web browser to make some containers for storing things:
In these examples, using var
or let
will produce the same result.
You will learn more about var
and let
later in this tutorial.
JavaScript Comments
Not all JavaScript commands are executed.
Computer code uses special symbols to create comments. Comments are messages within the code that are not meant to be executed. In code, if you write between two forward slashes //
or between /
and /
, it's treated as a comment and the computer ignores it when running the program.
Comments are not considered and won't be carried out:
You will learn more about comments in a later chapter.
JavaScript Identifiers / Names
Identifiers in JavaScript are simply names.
Identifiers are like names for variables, keywords, and functions.
The naming guidelines for legal names are consistent across many programming languages.
A JavaScript name must begin with:
- A letter (A-Z or a-z)
- A dollar sign ($)
- Or an underscore (_)
Following characters can be letters, numbers, underscores, or dollar signs.
Note
Numbers are not allowed as the first character in names.
In this way, JavaScript can easily tell apart names from numbers.
JavaScript is Case Sensitive
All JavaScript names are sensitive to letter case.
The words 'lastName' and 'lastname' represent distinct variables.
JavaScript doesn't treat LET or Let as the special let keyword.
JavaScript and Camel Case
In the past, programmers have employed various methods to combine multiple words into a single variable name in their code.
Hyphens:
First name, last name, MasterCard, intercity.
Hyphens are not allowed in JavaScript. They are reserved for subtractions.
Underscore:
first_name, last_name, master_card, inter_city.
Upper Camel Case (Pascal Case):
FirstName, LastName, MasterCard, InterCity.
Lower Camel Case:
JavaScript programmers tend to use camel case that starts with a lowercase letter:
firstName, lastName, masterCard, interCity.
JavaScript Character Set
JavaScript uses the Unicode character set with the same width and height settings as mentioned in the HTML tags and class attributes.
Unicode includes almost every character, punctuation mark, and symbol from around the world.