JavaScript 101: Into Programming

javascript-logo

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:

  1. 5 is a literal value expression
  2. y is a variable expression
  3. y + 5 is an arithmetic expression
  4. x = 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:

  1. + Adds two numeric operands.
  2. - Subtract right operand from the left operand
  3. * Multiply two numeric operands.
  4. / Divide left operand by the right operand.
  5. % Modulus operator. Returns remainder of two operands.
  6. ++ Increment operator. Increase operand value by one.
  7. -- Decrement operator. Decrease value by one.

Logical operators are used to combine two or more conditions. JavaScript includes the following logical operators.

  1. && 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.
  2. || 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).
  3. ! 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.

  1. == Compares the equality of two operands without considering type.
  2. === Compares equality of two operands with type.
  3. != Compares the inequality of two operands.
  4. > Checks whether left-side value is greater than right side value. If yes then returns true otherwise false.
  5. < Checks whether the left operand is less than the right operand. If yes then returns true otherwise false.
  6. >= Checks whether the left operand is greater than or equal to the right operand. If yes then returns true otherwise false.
  7. <= 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.

  1. = Assigns right operand value to left operand.
  2. += Sums up left and right operand values and assign the result to the left operand.
  3. -= Subtract right operand value from left operand value and assign the result to the left operand.
  4. *= Multiply left and right operand values and assign the result to the left operand.
  5. /= Divide left operand value by right operand value and assign the result to the left operand.
  6. %= 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.




#javascript #javascript101

Author: Aleksandar Vasilevski |

Loading...