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';
//Sau
let name ='Value';
- Example:
<h4>Example JS variable</h4>

<script>
let str ='Some text';
document.write(str);
</script>

The difference between var and let

The difference between var and let is scoping.


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

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

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

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

document.write(xn); // 2
</script>

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.
<script>
var x; //now is undefined

x = 5; //now is a number
document.write(x);

x = '<h4>MarPlo</h4>'; //now is a string
document.write(x);
</script>

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:
<script>
//a function
function f(){
 var x ='val';
}

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

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.
<script>
const TJC = 'JavaScript Tutorial - Constants';
document.write(TJC);
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark>coursesweb.net</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);
var_export($arr);
Defiing Variables and Constants

Last accessed pages

  1. Recursive function to create Multi-Level Menu in JavaScript (3542)
  2. Validate radio and checkbox buttons (6680)
  3. Adding data from HTML Table Rows in Form fields (6196)
  4. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (46913)
  5. PuzzleImg - Script to Create Image Puzzle Game (4126)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1431)
  2. Contact page - CoursesWeb (1396)
  3. Tabs effect with CSS (1392)
  4. PHP getElementById and getElementsByTagName (623)
  5. Get Attribute (ID, Class, Name, Title, Src) with jQuery (522)