JavaScript 101: Into Programming
Code
A program is often referred to as source code or just code. Program is a set of special instructions to tell the computer what tasks to perform. Code usually is saved to the file but with JavaScript, you can write code directly into a developer console in a browser.
The rules of combinations of instructions is called a computer language, sometimes referred to as its syntax.
Statements
In a computer language, inputs line numbers, and operators that perform a specific task is a statement. In JavaScript, a statement might look as follows:
x = y + 5
The characters x and y are called variables (see JavaScript 101: Variables and Data Types for more about variables), which are like containers where you can store values.
Number 5 is called a literal value because it stands alone without being stored in a variable.
The = and + characters are operators, they perform actions with the values and variables such as assignment and mathematic operation.
The statement x = y + 5; tells the computer, to get the current value stored in the variable y, add that value with 5, then store the result back into another variable x.
Programs are just collections of many such statements, which together describe all the steps that it takes to perform your program.
Expressions
Statements are made up of one or more expressions. An expression is any reference to a variable or value, or a set of variables and values combined with operators.
Here is an example:
x = y + 5
This statement has four expressions in it:
5
is a literal value expressiony
is a variable expressiony + 5
is an arithmetic expressionx = y + 5
is an assignment expression
Executing a Program
The program needs to be executed, also referred to as running the program.
For some computer languages, this translation of commands is typically done from top to bottom, line by line, every time the program is run, which is usually called interpreting the code.
For other languages, the translation is done ahead of time, called compiling the code, so when the program runs later, what's running is the already compiled computer instructions ready to go.
Output
Using console.log(..)
you can create output by printing the value to the console.
Another way of creating output that you can see is to run an alert(..)
statement.
Input
The most common way that happens is for the HTML page to show form elements to a user that they can type into, and then using JavaScript to read those values into your program's variables.
Operators
Operators are how we perform actions on variables and values.
There are two types of operators:
- Arithmetic Operators
- Logical Operators
- Comparison Operators
- Assignment Operators
- Conditional Operators
Arithmetic operators are used to perform mathematical operations between numeric operands. There are seven of them:
+
Adds two numeric operands.-
Subtract right operand from the left operand*
Multiply two numeric operands./
Divide left operand by the right operand.%
Modulus operator. Returns remainder of two operands.++
Increment operator. Increase operand value by one.--
Decrement operator. Decrease value by one.
Logical operators are used to combine two or more conditions. JavaScript includes the following logical operators.
&&
is known as AND operator. It checks whether two operands are non-zero (0, false, undefined, null or "" are considered as zero), if yes then return 1 otherwise 0.||
is known as OR operator. It checks whether any one of the two operands is non-zero (0, false, undefined, null or "" is considered as zero).!
is known as NOT operator. It reverses the boolean result of the operand (or condition).
Comparison operators are used to compare two operands and return Boolean value true or false.
==
Compares the equality of two operands without considering type.===
Compares equality of two operands with type.!=
Compares the inequality of two operands.>
Checks whether left-side value is greater than right side value. If yes then returns true otherwise false.<
Checks whether the left operand is less than the right operand. If yes then returns true otherwise false.>=
Checks whether the left operand is greater than or equal to the right operand. If yes then returns true otherwise false.<=
Checks whether the left operand is less than or equal to the right operand. If yes then returns true otherwise false.
Assignment operators are used to assign values to variables.
=
Assigns right operand value to left operand.+=
Sums up left and right operand values and assign the result to the left operand.-=
Subtract right operand value from left operand value and assign the result to the left operand.*=
Multiply left and right operand values and assign the result to the left operand./=
Divide left operand value by right operand value and assign the result to the left operand.%=
Get the modulus of left operand divide by the right operand and assign resulted modulus to the left operand.
Conditional operators are used to compare values, like short if-else condition.
<condition> ? <value1> : <value2>;
Next part: JavaScript 101: Variables and Data Types.
Tweet