JavaScript Tutorials
- What is Javascript?
- What is BOM?
- What is DOM?
- Variable
- check variable value is empty
- JavaScript Output
- Functions
- Javascript Events
- Input Events
- onchange Event
- Javascript Output methods
- If else statement
- Arrays
- Pattern Validation
- Form Validation
- Inner Html Form Validation
- Inline Form Validation
- Checkbox Validation
- Inline Inner Html Form Validation
- Server side php Validation
- Validate a HTML Form with PHP
- window and window location
- Get Text Value
- Get hidden field value
- JavaScript & PHP
- Date Format
- get php value in javascript
- Redirect page & Autoredirect page
- Auto Refresh page & Div
- How to get select text value from Dropdown box
- How to clear browser history in javascript
- Checkbox Problems
- Select option problems
- Popup Contact Form
- Sidebar Contact Form
- How to use a multistep Form or Form wizard
- Auto Calculate Price
- print Application Form
- Auto Calculate GST in Javascript by select price
- Calculate GST by input value in text box Jquery
- Calculate Discount
- onClick Checkbox
- autofil form data click on checkbox
- Show subcategory list
- Show city list as per state
- Show district list as per country and state
- Show good morning good night wish
- image upload with preview image
- Print Div Content
- Show modal popup on page load
- filter table data usign javascript.
- Character Limit Validation.
- Validate File 5MB Upload
- Validate Special character
- More File Upload
- Call JavaScript Function After Page Load
- Drop Down First option Value Disabled --- Please Select ---
- How to Disable Submit Button After Form Submission
- How to disable browser back button using Jquery?
- How to Remove selected item from second & third dropdown list?
- Interview Questions of JavaScript.
Important Link
What is 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:
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.
Output :-
<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.
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php