Let us explain some of the basic features of JavaScript to give you a better understanding of how it all works. It is worth mentioning that these features are common to all programming languages, so if you master these fundamentals, you will be on the way to programming almost anything!
1. Variables
Variables are containers in which you can store values. Start by declaring a variable using the keyword lime, followed by any name:
var myVariable;
A semicolon at the end of a line indicates where a statement ends; it is necessary when you have to separate statements on a single line. However, some people think it is good practice to put them at the end of each statement. You can call a variable just about anything, but there are some name restrictions.
JavaScript is case-sensitive – myVariable is a variable over my variable. If you have problems with your code, check the variable name.
The variables have different types of data:
1. String – A sequence of text known as a string. In order to indicate that the variable is a string, it should be enclosed in quotation marks.
Ex: var myVariable = ‘Tom’;
2. Number – A number. The numbers do not have quotation marks around them.
Ex: myVariable var = 9;
3. Boolean – A True / False value. The words true and false are special keywords in JS, and do not need quotation marks.
Ex: var myVariable = true;
4. Array – A structure that allows you to store multiple values in a single reference. Ex:
var myVariable = [2, ‘Tom’, ‘Sarah’, 10];
To refer to each member of the array, use: myVariable [0], myVariable [1], etc.
5. Objects – Practically anything. Everything in JavaScript is an object and can be stored in a variable. Keep this in mind as you learn.
Ex: var myVariable = document.querySelector (‘h1’);
Why do we need variables?
Well, variables are needed to make anything interesting in programming. If the values can’t be changed, then you could not do anything dynamic, such as customizing a welcome message or changing the image displayed in an image gallery.
2. Comments
You can include comments in the JavaScript code, as you can in CSS:
/ *
Everything is a comment.
* /
If your comment does not include breaks between the lines, it is often easier to put it after two slashes like these:
// This is a comment
3. Operators
An operator is a mathematical symbol that produces a result based on two values (or variables). In the following table, you can see some of the simplest operators, along with a few examples to try in the JavaScript console.
– Assembly. Used to gather two numbers together or merge two rows.
– Deduction, Multiplication, Dividing. They do what you expect them to do in basic math.
– Attribution. You have already seen this: assign a value to a variable.
– Equality. Take a test to see if two values are equal to each other and return a true/false (Boolean) result.
– Negation, Not equal. Returns the opposite logical value of the foregoing, turns a true value into a false value, etc. When used with the Equality operator, the negation operator tests whether two values are not equal.
Mixing data types can lead to strange results when making calculations, so be careful when referring to your variables to get the results you expect. For example, enter “35” + “25” in your console. Why don’t you get the expected result? Because quotation marks turn numbers into strings, and you end up concatenating strings instead of gathering numbers. If you enter 35 + 25, you will get the correct result.