JavaScript Variables - Java Script

What are JavaScript Datatypes?

Like other programming languages javascript also supports a set of datatypes.

Datatypes are the type of values that can be represented and manipulated in a programming language.

In JavaScript you are going to work with three primitive data types -

  • Numericals or numbers, eg. 123, 120.50 etc.
  • Strings of text e.g. "This text string" etc.
  • Boolean e.g. true or false.

Along with primitive data types,JavaScript also defines two trivial data types, null and undefined.

These trivial data types defines only a single value.

In addition to these primitive data types, JavaScript supports a composite data type known as object.

These objects will be covered in detail in a separate chapter.

Note - In JavaScript integer values and floating-point values makes no diffferece between them.

All numbers in JavaScript are represented as floating-point values.JavaScript uses 64-bit floating-point format to represent numbers. 64-bit floating-point format is defined by the IEEE 754 standard.

What are JavaScript Variables?

Variables can be defined as named containers for storing values which can be used later.For that You should place data into these containers and name the container.

Later you can refer the data whenever necessary by calling the container by its name.To use a variable in JavaScript program,you need to declare the variable priorly

Before you use a variable in a JavaScript program, you need to declare it.Variables are declared with the var keyword as follows.

multiple variables can also be declared with the same var keyword as follows -

Storing a value in a variable is called variable initialization.

You can initialize a variable at the time of creating it or later when you need that variable.

For example, lets consider that you have created a variable named as money, which can be assigned later

For another variable, you assigned a value at the time of initialization.

lets see the example as follows:

Note - In a document the var keyword is declared or initialized only once for the life of any variable name.

Same variable cannot be declared twice in a document.

JavaScript is untyped language. This means that a JavaScript variable can hold a value of any data type.

Unlike many other languages, you need not to tell JavaScript about the type of variable it holds during variable declaration.

The value type that a variable holds can change dynamically during the execution of a program and JavaScript takes care of it automatically.

What is JavaScript Variable Scope?

The scope of a variable can be defined as the region or area of your program in which it is defined.

JavaScript variables are provided only with two scopes.

  • Global Variables - A global variable has global scope, which means it can be declared anywhere in your javascript code.
  • Local Variables - A local variable will be visible and available only within a function where it is defined.

Function parameters will always be local to that function.

Suppose if you assign same name to both local and global variable,local variable with in a function takes precedence over global variable

If you declare a local variable or function parameter with the same name as a global variable,The global variable will be effectively hidden. Take a look into the following example.

The above produces the following result as "local".

What are JavaScript Variable Names?

While giving names to your variables in JavaScript, one should keep the following rules in mind.

  • JavaScript reserved keywords cannot be used as a variable names.
  • keywords are explained in the next section. For example, break or boolean cannot be used as variable names.
  • JavaScript variable names should not start numerals (0-9).
  • They must begin with a letter or an underscore character.
  • For example, 231test is an invalid variable name but _231test is a valid one.
  • JavaScript variable names are case-sensitive. For example, Name and name are two different variables.

What are JavaScript Reserved Words?

A list of all the reserved words in JavaScript are listed below.

JavaScript reserved words cannot be used as JavaScript functions, variables, loop labels,methods, 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


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Script Topics