***** On Monday, 9/10/18, I started learning the fundamentals of JavaScript on sololearn.com. *****
***** On Saturday, 1/12/19, I completed the JavaScript Tutorial course and was issued the certificate. *****
TAKE NOTE THAT THIS IS NOT A .JS FILE BECAUSE IT WILL BE A PLAIN TEXT FILE. INSTEAD, JAVASCRIPT WILL BE IMPLEMENTED INTO A .HTML FILE. THIS CAN BE DONE VIA INTERNAL JAVASCRIPT OR EXTERNAL JAVASCRIPT.
Variables are containers for storing data values.
The value of a variable can change
throughout the program.
Use the var keyword to declare a
variable:
i.e. → JavaScript Code in JS ←
var x = 10;
In the example above, the value 10 is assigned to the variable x.
The Equal Sign
In JavaScript, the equal sign (=) is called the
"assignment" operator, rather than an "equal to" operator.
For example, x = y will assign the value of
y to x.
Using Variables
Let's assign a value to a variable and output it to the browser.
i.e. → JavaScript Code in JS ←
var x = 100;
document.write(x);
→ ←
Using variables is useful in many ways. You might have a thousand lines of code that may include the variable x. When you change the value of x one time, it will automatically be changed in all places where you used it.
Naming Variables
JavaScript variable names are case-sensitive.
In the example below, we changed x to uppercase.
i.e. → JavaScript Code in JS ←
var x = 100;
document.write(X);
This code will not result in any output, as x and X are two different variables.
Naming rules:
There are some other rules to follow when naming your JavaScript variables:
The term data type refers to the types of values with which a program can work. JavaScript variables can hold many data types, such as numbers, strings, arrays, and more.
Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point, etc.
JavaScript numbers can be written with or without decimals.
i.e. → JavaScript Code in JS ←
var num = 42; // A number without decimals
Floating-Point Numbers
JavaScript numbers can also have decimals.
i.e. → JavaScript Code in JS ←
<script>
var price = 55.55;
document.write(price);
</script>
→ ←
Strings
JavaScript strings are used for storing and manipulating text.
A string can be any text that appears within
quotes. You can use single or double quotes.
i.e. → JavaScript Code in JS ←
var name = 'John';
var text = "My name is John Smith";
You can use quotes inside a string, as long as they don't match the quotes surrounding the string.
i.e. → JavaScript Code in JS ←
var text = "My name is 'John' ";
As strings must be written within quotes, quotes inside the string must be handled. The backslash (\) escape character turns special characters into string characters.
i.e. → JavaScript Code in JS ←
var sayHello = 'Hello world!
\'I am a
JavaScript programmer.\' ';
document.write(sayHello);
→ ←
The escape character (\) can also be used to insert other special
characters into a string.
These special characters can be added to a text string
using the backslash sign.
Booleans
In JavaScript Boolean, you can have one of two values, either true
or false.
These are useful when you need a data type that can only have one of two values,
such as Yes/No,
On/Off,
True/False.
Example: (JavaScript Code in JS)
var isActive = true;
var isHoliday = false;
Arithmetic Operators
Arithmetic operators perform arithmetic functions on numbers (literals or variables).
In the example below, the addition operator is used to determine the sum of two numbers.
i.e. → JavaScript Code in JS ←
var x = 10 + 5;
document.write(x);
// Outputs 15
→ ←
You can add as many numbers or variables together as you want or need to.
i.e. → JavaScript Code in JS ←
var x = 10;
var y = x + 5 + 22 + 45 + 6548;
document.write(y);
// Outputs 6630
→ ←
Multiplication
The multiplication operator (*) multiplies one number by the other.
i.e. → JavaScript Code in JS ←
var x = 10 * 5;
document.write(x);
// Outputs 50
→ ←
Division
The / operator is used to perform division operations.
i.e. → JavaScript Code in JS ←
var x = 100 / 5;
document.write(x);
// Outputs 20
→ ←
The Modulus
Modulus (%) operator returns the division remainder (what is left over).
i.e. → JavaScript Code in JS ←
var myVariable = 26 % 6;
document.write(myVariable);
// myVariable equals 2
→ ←
Increment & Decrement
Increment ++
The increment operator increments the numeric value of its operand by one. If placed
before the operand, it returns the incremented value. If placed after the operand,
it returns the original value and then increments the operand.
Deccrement --
The decrement operator decrements the numeric value of its operand by one. If placed
before the operand, it returns the decremented value. If placed after the operand,
it returns the original value and then decrements the operand.
Some examples:
Assignment operators assign values to JavaScript variables.
Comparison operators are used in logical statements to determine equality or difference between variables or values. They return true or false.
The equal to (==) checks whether the operands' values are equal.
i.e. → JavaScript Code in JS ←
var num = 10;
document.write(num == 8);
// num == 8 will return false
→ ←
The table below explains the comparison operators.
Logical Operators, also known as Boolean Operators, evaluate the expression and return true or false.
The table below explains the logical operators (AND, OR, NOT).
In the following example, we have connected two Boolean expressions with the AND operator.
i.e. → JavaScript Code in JS ←
document.write((4 > 2) && (10 < 15));
→ ←
For this expression to be true, both conditions must be true.
Conditional (Ternary) Operator
Another JavaScript conditional operator assigns a value to a variable, based on some condition.
Syntax:
variable = (condition) ? value1: value2
For example:
var isAdult = (age < 18) ? "Too young": "Old enough";
If the variable age is a value below 18, then the
value of the variable isAdult will be
"Too young".
Otherwise the value of isAdult will be "Old enough".
The most useful operator for strings is concatenation, represented by the
+ sign.
Concatenation can be used to build strings by joining together multiple strings, or
by joining strings with other types:
i.e. → JavaScript Code in JS ←
var mystring1 = "I am learning";
var mystring2 = "JavaScript with SoloLearn";
document.write(mystring1 + mystring2);
→ ←
The above example declares and initializes two string variables, and then concatenates them.
Go to top
« Previous
1
2
3
4
5
6
7
8
Next »