Sunday, December 30, 2018

JavaScript Variable



Hello guy! evening and welcome to my new tutorial post on JavaScript. In my last tutorial i talked about Javascript placement, in this tutorial we will be talking about JavaScript Variables.

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

JavaScript allows you to work with three primitive data types:
  • Numbers eg 123, 150.50 etc.
  • Strings of text eg "this text is string" etc.
  • Boolean eg true or false.
JavaScript also defines two trivial data types, null and undefined, each of which defines only a single value.

In addition to these primitive data types, JavaScript support a composite data type known as object. We will talk about an object in detail in a separate tutorial.


You can also read: Javascript Syntax

Note: Java 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 the IEEE 754 standard.




JavaScript Variables:
Like many other programming languages, JavaScript has variables. Variables can be thought 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 first declare it. Variables are declared with the var keyword as follows:

<script  type="text/javascript">
<!--
var money;
var name;
//-->
</script>

You can also declare multiple variables with the same var keyword as follows:

<script  type="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 later point when you need that variable as follows:

For instance, you might create a variable named money and assign the value 5000.70 to it later. For another variable you can assign a value the time of initialization as follows:

<script  type="text/javascript">
<!--
var name ="Nova";
var money;
money=5000.70;
//-->
</script>

Note: Use the var keyword only for declaration or initialization. Once for a life of a variable name in a document. You should not re-declare same variable twice.


JavaScript is untyped language. This means that a JavaScript variable can hold a value of any data type. Unlike many other languages, you don't have to tell JavaScript during variable declaration what type of value the variable will hold. The value type of a variable can change during the execution of a program and JavaScript takes care of it automatically.


You can also read: How to enable Javascript


JavaScript Variable Scope:
The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.

  • Global Variables: A global variable has a global scope which means it is defined every where in your JavaScript code.
  • 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. Below is an example:


<script  type="text/javascript">

<!--
var myVar = "global";  // Global variable declaration
function checkscope()  {
var myVar = "local";  // Local variable declaration
    document.write(myVar);
}
//-->
</script>




JavaScript Variable Names:
While naming your variables in JavaScript keep the following rules in mind:

  • You should not use any of the JavaScript reserved keyword as a variable name. This keywords are listed out below. For example break or import variable names are invalid.
  • JavaScript variable names should not start with numeral (0-9). They must begin with a letter or the underscore character. For example, 123test is an invalid variable name but _123test is a valid variable name.
  • JavaScript variable names are case sensitive. For example name and Name are two different variables.
JavaScript Reserved Words:
The following are reserved words in javascript. They cannot be used as variable names, functions, methods, loops labels, or any object names.
abstract
boolean
break
byte
case
catch
char
class
const
continue
debugger
default
delete
do
double
else
enum
export
extends
false
final
finally
float
for
function
goto
if
implements
import
in
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
super
switch
synchronized
this
throw
throws
transient
true
try
typeof
var
void
volatile
while
with

Alright guys we have come to the end of this tutorial post on JavaScript variable, see you in my next tutorial on JavaScript and thanks for reading. Feel free to drop your questions in the comment box below, bye for now.

Like our facebook page and subscribe with us to get our tutorial posts delivered directly to your emails. Also share this tutorial post on the various social media platforms available.

You can follow us on twitter 
You can follow us on google+ 
You can also follow us on pinterest
Share:

0 comments:

Post a Comment