DECLARATION AND INITIALIZATION
As we discussed
introductions and some main important of JavaScript in programming word for beginners.
Now in second lecture we are discussing about some new points like basics in JavaScript
, declaration on JavaScript etc.
Before we use a variable in a JavaScript program,
we must declare it. Variables are declared with the var keyword. For example:
var age;
var name;
And also we can use
only keyword for more variables as:
var age, name;
Storing a value in a variable is called variable initialization. We can do variable initialization at the time of
variable creation or at a later point in time when we need that variable.
For instance, we might
create a variable named money and assign the value 24 to it later. For another
variable, we can assign a value at the time of initialization as follows:
var name = "Mahesh Prashad Joshi";
var age;
age = 24;
We should remember that
the var keyword only for declaration or initialization, once for the life of
any variable name in a document. We should not re-declare same variable twice.
If we are declaring the
variable in JavaScript we should use the variables properly. For example:
These declarations will
so the result as:
If we interchange the
declaration steps in another way like:
<script>
alert(x);
x="Mahesh Prashad Joshi"
var x;
</script>
This result as:
Reference
Error and Undefined Basically we may face two kinds of
errors Reference while declaration of the variable. Reference
error arises if we have not declared a variable but we are trying to use it. Undefined
is the case when we try to use a variable that we have declared but have not
assigned any value to it.
<script>
document.writeln(x);
</script>
This shows the reference error while run in
the browser as shown as below:
This declaration
creates the undefined type of error in JavaScript. And shows the result as:
Data
Types in JavaScript:
Basically JS has 8
basic data types:
SN |
Data Type |
Work As |
1 |
number |
variable
with number data type stores integer or floating-point values. |
2 |
bigint |
integer
numbers(of arbitrary length). |
3 |
string |
For
string values. |
4 |
boolean |
True/false. |
5 |
null |
Unknown
values |
6 |
undefined |
Unassigned
value. |
7 |
object |
Complex
data structures. |
8 |
symbol |
Unique
Identifier. |
And the three data
types string, number, boolean are primitive data types. Here is the method to
declare of these data type in programming language:
From Above program we
can conclude the result as:
0 comments:
Post a Comment