Datatypes and Variables in java script

Datatypes are used to hold the values of variables. JavaScript has two types of datatypes.

·       Primitive data types

·       Non-primitive data types

Java script is a dynamic type. It is not important to mention the datatype earlier. Use “var” for variable.

Primitive data types:

Javascript has 5 primitive data types. They are listed below.

Boolean: it represents zero or one value.

Number: it represents numbers.

String :it represents sequence of characters.

Null : it represents no value.

Undefined: if you want to define the value later, use this data type.

Non- primitive data types:

Javascript has 3 different types of non-primitive data types. They are given below.

Array: It has a list of similar values

Object: it is a instance of a class.

Regexp: It is for Regular Expressions.

 

Variables:

 Variables are so important for any language. It specifies the value. In javascript, a key word ”var” is used.

Syntax:

var variablename=value;

Eg:

//assigning numbers

var a=10;

//assigning string

var name=”Jey”;

note: variable name should start with alphabets(lowercase or uppercase), underscore(_),dollar($). These variables are case sensitive.

Variables can be categorized into two. They are

Local variables: Variable’s scope is within the block.

Global variables: Variable’s scope is for the entire module.

Examples are shown as below…

Addition of three numbers in javascript(Eg: local variable)

<!DOCTYPE html>

<html>

<head> </head>

<body>

<script language="javascript">

var a=10; // local variable

var b=3; // local variable

var c=5; // local variable

var d=a+b+c;

document.writeln("The sum is:"+d);

</script>

</body>

</html>

Output:

 


Pop up message display in javascript (Eg: Global variable)

<html>

<body>

<script> 

var value="good vibes";//global variable 

function a1(){ 

alert(value); 

} 

a1();

</script> 

</body>

</html>

Output:

 


 Thus,the Data types and variables used in java script is explained with examples.

No comments:

Post a Comment