Learn Javascript || VARIABLES AND DATATYPES with Examples

Learn Javascript || VARIABLES AND DATATYPES with Examples

One if the most fundamental characteristics of a programming language is the set of data types it supports. These are the type of values that can be represented and manipulated in a programming language.

In JavaScript Three primitive data types :

  1. Numbers, ex. 123, 120.50 etc.
  2. Strings of text ex. “This text string” etc.
  3. Boolean e.x. true or false.

JavaScript also defines two trivial data types, null and undefined, each of which define only a single value. In addition to these primitive data types, JavaScript support a composite data type know as object. JavaScript does not make a distinction between integer values and floating-point values. All numbers in JavaScript are represented as floating-point values . JavaScript represents numbers using the 64-bit floating-point format defined by IEEE 754 standard.

WHAT IS THE VARIABLES : Like many other programming language, JavaScript has variables. Variables can be thought of as named containers. You can place data into these containers and then refer to the data simply by naming the container. Before you use a variable in a JavaScript program, you must declare it. variables are declared with the “var” keyword.

EXAMPLE:

<script type=”text/javascript”>

var name = “Ali”;

var money;

money = 2000.50;

</script>

 

JAVASCRIPT VARIABLE SCOPE :

The scope of a variable is the region of your program in which it is defined. JavaScript variables have only two Scopes

1.Global Variables :- A global Variable has global scope which means it can be defined anywhere in your JavaScript code.

2.Local Variable :- A Local Variable will be visible only within a function where it is defined. Function parameters are always local to that function.

In the body of a function, Local Variables 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 example

EXAMPLE :

<html>

<body onload = checkscope();>

<script type = “text/javascript”>

var myVar = “global”;

function checkscope( ) {

var myVar = “local”;

document.write(myVar);

}

</script>

</body>

</html>

See More: JavaScript Introduction with Hello World! Example

About the author

admin

Leave a Comment