In this tutorial you will learn the basics of programming, which are required to write a script. These notions are similar to those of PHP and other programming languages.

1. Using variables

In a script (or program) we use constants and variables datas. The variables can change their values during program execution. These data are called "variables".

The simplest way to use and refer to a variable is to write it. The name of the variable permits the access at its value and also can change the value if necessary.
You can create a variable and assign a value to it in this way:
- With statement var
var name = value;
- You can change its value just using the variable name (without "var"):
name = other_value;
Variable Types - Unlike other languages (such as Pascal or C), JavaScript has no fixed types of data, that allows you to change the type of a variable in the script, it can recognizes when the data is a string, numerical or other type.
For example:
var x = "xyz";
x = 8;
- Notice that the 'string' values (consisting of letters) are written between quotation marks (simples or doubles), and the 'number' can be written without quotation marks.

The life spam of a variable - A variable written within a function is a local variable, its value is recognized only within that function, it doesn't exist out of that function. Thus, another function can declare a variable with same name, JS (JavaScript) treats the two as different variables ( functions and working with it will be explained in a subsequent lessons ).
The variables created out of the functions can be used everywhere in that script, inclusively inside the functions and other JS script from that HTML page.

2. Operators

To work with datas in a script and manipulate variable values we use operators.
Operators are symbols and identifiers that determines how data are modified and how it is evaluated a combination of expressions and variables.

JavaScript recognizes: Operators are several types: These will be detailed in the next lesson.

Daily Test with Code Example

Which HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark></mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicate the PHP function which reads an entire file into an array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Variables and Operators

Last accessed pages

  1. Get Duration of Audio /Video file before Upload (5913)
  2. Follow the mouse cursor with a DIV inside a Parent (2703)
  3. Register and show online users and visitors (23278)
  4. Node.js Move and Copy Directory (1127)
  5. SHA1 Encrypt data in JavaScript (8256)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (390)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (332)
  3. Register and show online users and visitors (302)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (234)
  5. Insert, Select and Update NULL value in MySQL (223)