Javascript Course

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'.
- A variable is a name of a location in computer memory, used to store data.

Using variables

In a script (or program) we use variables and constants datas. The variables can change their values 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 using the var or let declarations:
var name ='Value';
let name ='Value';
- Example:
<h4>Example JS variable</h4>

let str ='Some text';

The difference between var and let

The difference between var and let is scoping.

- Se the following examples.
1) Example with "let":
let xn = 1;

if(xn ==1){
 let xn =2;

document.write(xn); // 1
2) Example with "var":
var xn = 1;

if(xn ==1){
 var xn =2;

document.write(xn); // 2

Variable Types

There are several types of data that can be assigned to variables. The type of the value it determines the type of the variable.

JavaScript has no fixed types of data, it allows you to change the type of a variable in the script, it can recognize when the data is a string, numerical or other type.
var x; //now is undefined

x = 5; //now is a number

x = '<h4>MarPlo</h4>'; //now is a string

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, JavaScript treats the two as different variables.
- Example:
//a function
function f(){
 var x ='val';

document.write(x); //Error: x is not defined

Defining and Using Constants

Constants are defined with the const declaration.
Unlike variables, the value of a constant can not be changed and can not be redefined, its value remains the same, fixed.

const X = 'MarPlo';

//trying to change the value it results error in browser console
X ='abc';

//redefining, it results error in browser console
const X = 123;
Like variables, constants are used by specifying their names, and it results its value.
const TJC = 'JavaScript Tutorial - Constants';

Daily Test with Code Example

Which type of <input> creates a date input control, such as a pop-up calendar?
type="text" type="date" type="button"
<input type="date" name="set_date" value="2012-10-15" />
Which CSS property adds shadow effects to the text of an element?
font-style color text-shadow
h2 {
  text-shadow: 2px 3px 3px #a0a1fe;
Click on the function that adds new elements to the end of an array.
pop() shift() push()
var pags = ["lessons", "courses"];
pags.push("download", "tutorials");
alert(pags[2]);            // download
Which function sorts an array by key, in ascending order, maintaining key to data correlations?
asort() ksort() sort()
$lang =[10=>"PHP", 20=>"JavaScript", "site"=>"");
var_export($lang);     // array ("site"=>"", 10=>"PHP", 20=>"JavaScript")
Defiing Variables and Constants

Last accessed pages

  1. JavaScript Course - Free lessons (29697)
  2. Ajax click Tracking - Monitor clicks on html elements (1749)
  3. Get Lower, Higher, and Closest Number (4535)
  4. Wake Up! (14406)
  5. PHP Unzipper - Extract Zip, Rar Archives (20281)

Popular pages this month

  1. PHP Unzipper - Extract Zip, Rar Archives (162)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (98)
  3. SHA1 Encrypt data in JavaScript (77)
  4. Read Excel file data in PHP - PhpExcelReader (73)
  5. Create simple Website with PHP (70)