Js-Variables-and-Data-Type 3
Js-Variables-and-Data-Type 3
Topperworld.in
❖ JavaScript Variable
There are some rules while declaring a JavaScript variable (also known as
identifiers).
2) After first letter we can use digits (0 to 9), for example value1.
3) JavaScript variables are case sensitive, for example x and X are different
variables.
var x = 10;
var _value="sonoo";
var 123=30;
var *aa=320;
©Topperworld
JavaScript
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
Output:
30
• Global Variable
• Local Variable
©Topperworld
JavaScript
For example:
<script>
function abc(){
</script>
Or,
<script>
If(10<13){
</script>
A variable i.e. declared outside the function or declared with window object
is known as global variable.
©Topperworld
JavaScript
For example:
<script>
function a(){
document.writeln(data);
function b(){
document.writeln(data);
b();
</script>
To declare JavaScript global variables inside function, you need to use window
object.
For example:
window.value=90;
Now it can be declared inside any function and can be accessed from any
function.
©Topperworld
JavaScript
For example:
function m(){
window.value=100;
function n(){
alert(window.value);
When you declare a variable outside the function, it is added in the window
object internally. You can access it through window object also.
For example:
var value=50;
function a(){
alert(window.value);
©Topperworld
JavaScript
DataTypes
❖ Data Types
• JavaScript is a dynamically typed (also called loosely typed) scripting
language.
• The latest ECMAScript standard defines eight data types Out of which
seven data types are Primitive(predefined) and one complex or Non-
Primitive.
◆ Boolean: Represent a logical entity and can have two values: true or
false.
©Topperworld
JavaScript
The data types that are derived from primitive data types of the JavaScript
language are known as non-primitive data types.
◆ Object: It is the most important data type and forms the building
blocks for modern JavaScript.
©Topperworld