DAY-3
JavaScript Data types
Ø In computer programming language a data type is a
classification identifying one of various type of data such as real, integer or
boolean, that determines the possible values for that the, the operations that
can be done on values of that type, the meaning of the data and the way the
values of that type can be stored.
Ø JavaScript has six data types and there are Numbers,
Strings, Boolean, Null, Undefined and Objects.
Ø The following data types are called primitive data
types −
·Numbers, eg. 123, 120.50 etc.
·Strings of text e.g. "This text string" etc.
·Boolean e.g. true or false.
Ø The null and undefinedare called trivial data
types, each of which defines only a single value and object is called as composite data type.
Number Datatype
Ø JavaScript does not make a distinction between integer
values and floating-point values. All numbers in JavaScript are represented as 64
bit floating-point valuesformat defined by the IEEE 754 standard.
Ø JavaScript has some built in function to work with
numeric values. isNaN() is a function to check whether a value is number or
not.
JavaScript Variables
Ø Variables can be thought of as named containers.
Ø Before use a variable in a JavaScript program, you
must declare it. Variables are declared with the var keyword. For example:
<scripttype="text/javascript">
<!--
var money;
var name;
//-->
</script>
Ø You can also declare multiple variables with the
same var keyword as
follows −
<scripttype="text/javascript">
<!--
var money, name;
//-->
</script>
Ø Storing a value in a variable is called variable initialization. You can do
variable initialization at the time of variable creation or at a later point in
time when you need that variable. For example
<scripttype="text/javascript">
<!--
var name ="Ali";
var money;
money=2000.50;
//-->
</script>
Ø The scope of a variable is the region of your program
in which it is defined. JavaScript variables have only two scopes.
·Global Variables − A global variable has global scope which is
declared outside function and that can be accessed by every function.
·Local Variables − A local variable will be visible only within a
function where it is defined. Function parameters are always local to that
function.
Ø Within the body of a function, a local variable takes
precedence over a global variable with the same name. If you declare a local
variable or function parameter with the same name as a global variable, you
effectively hide the global variable. Take a look into the following example.
<html>
<bodyonload=checkscope();>
<scripttype="text/javascript">
<!--
varmyVar="global";// Declare a global
variable
functioncheckscope(){
varmyVar="local";// Declare a local
variable
document.write(myVar);
}
//-->
</script>
</body>
</html>
Ø Don’t use any of the JavaScript reserved keywords as a
variable name.
Ø JavaScript variable names should not start with a
numeral (0-9). They must begin with a letter or an underscore character. For
example, 123test is an
invalid variable name but _123test is
a valid one.
Ø JavaScript variable names are case-sensitive. For
example, Name and name are two different variables.
JavaScript Reserved Words
A list of all the reserved
words in JavaScript are given in the following table. They cannot be used as
JavaScript variables, functions, methods, loop labels, or any object names.
abstract
|
else
|
instanceof
|
switch
|
boolean
|
enum
|
int
|
synchronized
|
break
|
export
|
interface
|
this
|
byte
|
extends
|
long
|
throw
|
case
|
false
|
native
|
throws
|
catch
|
final
|
new
|
transient
|
char
|
finally
|
null
|
true
|
class
|
float
|
package
|
try
|
const
|
for
|
private
|
typeof
|
continue
|
function
|
protected
|
var
|
debugger
|
goto
|
public
|
void
|
default
|
if
|
return
|
volatile
|
delete
|
implements
|
short
|
while
|
do
|
import
|
static
|
with
|
double
|
in
|
super
|
Practical
Write
JavaScript code to create variables.
No comments:
Post a Comment
Give your valuable feedback