Variables are containers in which you can store values. Start by declaring a variable using the keyword lime, followed by any name:
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.
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 , myVariable , etc.
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.
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
– 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.