what is variable ?
how to create variable in javascript?
You can create a variable with the var keyword, whereas the assignment operator (=) is used to assign value to a variable, like this: var varName = value;
File name : index.php
var name = "Mahtab Habib";
var age = 18;
var isMarried = false;
// Declaring Variable
var userName;
// Assigning value
userName = "Mahi";
there are three primitive data types:
Numbers
Strings
Boolean
JavaScript also defines two trivial data types, null and undefined, each of which defines only a single value.
JavaScript supports a composite data type known as object.
Javascript Variable.
In javascript, variable is define by var keyword.
Example
File name : index.php
<script>
var a = 10
var b = 15
var c = 50;
// You can also declare multiple variables with the same var keyword
var val = 50, 100;
var a = 10, var b = 15;
var name = "mahtab";
</script>
Note :- no need to use semicolon in end of statement in javascript.
A variable can have variable values during the execution of a JavaScript. A literal is always a constant value.
A variable is a name. A literal is value.
JavaScript Variable Scope:
There are two types of variable scope.
local : A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
global :- A global variable has global scope which means it can be defined anywhere in your JavaScript code.
Output :-
<html>
<body onload = checkscope();>
<script type = "text/javascript">
var myVar = "global"; // Declare a global variable
function checkscope( ) {
var myVar = "local"; // Declare a local variable
document.write(myVar);
}
checkscope();
</script>
</body>
</html>
Example :-
File name : index.php
<html>
<body>
<h1>JavaScript Variables Example</h1>
<p>Create a variable, assign a value to it, and display it:</p>
<p id="mydemo"></p>
<script>
var bikename = "Yamha";
document.getElementById("mydemo").innerHTML = bikename;
</script>
</body>
</html>
A globally-scoped variable
File name : index.php
<script>
var a = 5; //global scope
function one() {
alert(a);
}
one();
</script>
Local scope
File name : index.php
<script type="text/javascript">
var a = 1;
function two(a) {
alert(a); // alerts the given argument, not the global value of '1'
// variable a undefined.
}
// local scope again
function three() {
var a = 3;
alert(a); // alerts '3'
}
two();
three();
</script>
File name : index.php
<script type="text/javascript">
var a = 1;
function four() {
if (true) {
var a = 4;
}
alert(a); // alerts '4', not the global value of '1'
}
four();
</script>
NOTE :-
File name : index.php
// If you don't declare your local variables with the var keyword, they are part of the global scope
var name = "Michael Jackson";
function showCelebrityName () {
console.log (name);
}
function showOrdinaryPersonName () {
name = "Johnny Evers";
console.log (name);
}
showCelebrityName (); // Michael Jackson
// name is not a local variable, it simply changes the global name variable
showOrdinaryPersonName (); // Johnny Evers
// The global variable is now Johnny Evers, not the celebrity name anymore
showCelebrityName (); // Johnny Evers
// The solution is to declare your local variable with the var keyword
function showOrdinaryPersonName () {
var name = "Johnny Evers"; // Now name is always a local variable and it will not overwrite the global variable
console.log (name);
}
If a variable is initialized (assigned a value) without first being declared with the var keyword, it is automatically added to the global context and it is thus a global variable:
File name : index.php
function showAge () {
// Age is a global variable because it was not declared with the var keyword inside this function
age = 90;
console.log(age);//
}
showAge (); // 90
// Age is in the global context, so it is available here, too
console.log(age); // 90
File name : index.php
<script type="text/javascript">
function valget() {
name = "mahtab";
}
alert(name);
</script>
setTimeout Variables are Executed in the Global Scope
Note that all functions in setTimeout are executed in the global scope.
File name : index.php
// The use of the "this" object inside the setTimeout function refers to the Window object, not to myObj
var highValue = 200;
var constantVal = 2;
var myObj = {
highValue: 20,
constantVal: 5,
calculateIt: function () {
setTimeout (function () {
console.log(this.constantVal * this.highValue);
}, 2000);
}
}
// The "this" object in the setTimeout function used the global highValue and constantVal variables, because the reference to "this" in the setTimeout function refers to the global window object, not to the myObj object as we might expect.
myObj.calculateIt(); // 400
// This is an important point to remember.
how to create variable in javascript?
You can create a variable with the var keyword, whereas the assignment operator (=) is used to assign value to a variable, like this: var varName = value;
File name : index.php
var name = "Mahtab Habib";
var age = 18;
var isMarried = false;
// Declaring Variable
var userName;
// Assigning value
userName = "Mahi";
there are three primitive data types:
Numbers
Strings
Boolean
JavaScript also defines two trivial data types, null and undefined, each of which defines only a single value.
JavaScript supports a composite data type known as object.
Javascript Variable.
In javascript, variable is define by var keyword.
Example
File name : index.php
<script>
var a = 10
var b = 15
var c = 50;
// You can also declare multiple variables with the same var keyword
var val = 50, 100;
var a = 10, var b = 15;
var name = "mahtab";
</script>
Note :- no need to use semicolon in end of statement in javascript.
A variable can have variable values during the execution of a JavaScript. A literal is always a constant value.
A variable is a name. A literal is value.
JavaScript Variable Scope:
There are two types of variable scope.
local : A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
global :- A global variable has global scope which means it can be defined anywhere in your JavaScript code.
Output :-
<html>
<body onload = checkscope();>
<script type = "text/javascript">
var myVar = "global"; // Declare a global variable
function checkscope( ) {
var myVar = "local"; // Declare a local variable
document.write(myVar);
}
checkscope();
</script>
</body>
</html>
Example :-
File name : index.php
<html>
<body>
<h1>JavaScript Variables Example</h1>
<p>Create a variable, assign a value to it, and display it:</p>
<p id="mydemo"></p>
<script>
var bikename = "Yamha";
document.getElementById("mydemo").innerHTML = bikename;
</script>
</body>
</html>
A globally-scoped variable
File name : index.php
<script>
var a = 5; //global scope
function one() {
alert(a);
}
one();
</script>
Local scope
File name : index.php
<script type="text/javascript">
var a = 1;
function two(a) {
alert(a); // alerts the given argument, not the global value of '1'
// variable a undefined.
}
// local scope again
function three() {
var a = 3;
alert(a); // alerts '3'
}
two();
three();
</script>
File name : index.php
<script type="text/javascript">
var a = 1;
function four() {
if (true) {
var a = 4;
}
alert(a); // alerts '4', not the global value of '1'
}
four();
</script>
NOTE :-
File name : index.php
// If you don't declare your local variables with the var keyword, they are part of the global scope
var name = "Michael Jackson";
function showCelebrityName () {
console.log (name);
}
function showOrdinaryPersonName () {
name = "Johnny Evers";
console.log (name);
}
showCelebrityName (); // Michael Jackson
// name is not a local variable, it simply changes the global name variable
showOrdinaryPersonName (); // Johnny Evers
// The global variable is now Johnny Evers, not the celebrity name anymore
showCelebrityName (); // Johnny Evers
// The solution is to declare your local variable with the var keyword
function showOrdinaryPersonName () {
var name = "Johnny Evers"; // Now name is always a local variable and it will not overwrite the global variable
console.log (name);
}
If a variable is initialized (assigned a value) without first being declared with the var keyword, it is automatically added to the global context and it is thus a global variable:
File name : index.php
function showAge () {
// Age is a global variable because it was not declared with the var keyword inside this function
age = 90;
console.log(age);//
}
showAge (); // 90
// Age is in the global context, so it is available here, too
console.log(age); // 90
File name : index.php
<script type="text/javascript">
function valget() {
name = "mahtab";
}
alert(name);
</script>
setTimeout Variables are Executed in the Global Scope
Note that all functions in setTimeout are executed in the global scope.
File name : index.php
// The use of the "this" object inside the setTimeout function refers to the Window object, not to myObj
var highValue = 200;
var constantVal = 2;
var myObj = {
highValue: 20,
constantVal: 5,
calculateIt: function () {
setTimeout (function () {
console.log(this.constantVal * this.highValue);
}, 2000);
}
}
// The "this" object in the setTimeout function used the global highValue and constantVal variables, because the reference to "this" in the setTimeout function refers to the global window object, not to the myObj object as we might expect.
myObj.calculateIt(); // 400
// This is an important point to remember.
let keyword :-
let keyword is used for declaring a variable.
The let keyword was introduced in ES6 (2015).
Variables defined with let keyword cannot be Redeclared.
Variables defined with let must be Declared before use.
Variables defined with let have Block Scope.
File name : index.php
let x = "Sana Mahtab";
let x = "Mahira Mahtab";
// SyntaxError: 'x' has already declared.
Note :- you can redeclared variable with var keyword
var x = "sana mahtab";
var x = "Mahira mahtab";
// no syntax error.
Block Scope
Before ES6 (2015), JavaScript had only Global Scope and Function Scopebr/>.
ES6 introduced two important new JavaScript keywords: let and const.br/>
These two keywords provide Block Scope in JavaScript.br/>
Variables declared inside a { } block cannot be accessed from outside the block:
File name : index.php
{
let x = 7;
}
// x can NOT be used here because it declared with let.
Note :- when variable is declared with var then it access outside the block.
{
var x = 7;
}
// x can be accessed out side the block
Redeclaring Variables
Redeclaring a variable inside a block will also redeclare the variable outside the block:
File name : index.php
<p id="demo"></p>
<script>
var x = 10;
// Here x is 10
{
var x = 2;
// Here x is 2
}
// Here x is 2
document.getElementById("demo").innerHTML = x;
</script>
Output :- x = 2
Redeclaring Variables with let
File name : index.php
<p id="demo"></p>
<script>
let x = 10;
// Here x is 10
{
let x = 2;
// Here x is 2
}
// Here x is 10
document.getElementById("demo").innerHTML = x;
</script>
Output :- x = 10
Previous
Next