Javascript Data Types

Javascript has the following data types

  • Number : This can be either integer or real.
  • Boolean : True or False values.
  • String : A group of charecters between quotes.
  • Null : This is keword to define a Null value.

Variables in Javascript need not be defined as any data type when they are decleared.

JavaScript Variables

Variables are naming containers used to storing data value 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 as follows

<script type="text/javascript">
var isValid = false;
var customerName = "Charlie";
var customerId = 212312;
</script>

Since Javascript is loosely typed, conversion can be done from one type to another type automatically. For Example : "isValid" having a boolean value may be re-assigned a value which is of type string. For Example: isValid = "This is example";

<script type="text/javascript">
var isValid = false;
var customerName;
var customerId;
customerName = "Tom";
customerId = 007;
</script>
<script type="text/javascript">
var customerName, customerAddress;
</script>

JavaScript Variable Declaration Rules

  • Can not use JavaScript reserved keyword as variable name. For example, break or boolean variable names are not valid.
  • JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 000Var is an invalid variable name but _000Var is a valid one.
  • JavaScript variable names are case sensitive. For example, Customer and customer are two different variables.

JavaScript Variable Scope

The scope of a variable is the region of source code in which it is defined. In Javascript a global variable has global scope; it is defined outside function in JavaScript code. On the other hand, variables declared within a function are defined only within the body of the function. They are local variables and have local scope.

Within the body of a function, a local variable takes precedence over a global variable if decleaded with same name. If variables are declared as local or function parameter with the same name as a global variable, effectively hide the global variables:

<script type="text/javascript">
var gscope = "global";   // This is a global variable
function checkVariableScope() {
    var gscope = "local";  // This is a local variable with the same name
    return gscope;  // This will return the local value, not the global one
}
checkVariableScope();   // "local"
</script>

JavaScript Reserved Words

Javascript has following reserved words. They cannot be used as variables name, functions name, methods name, loop 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

See Also

Javascript Built in String Object
Javascript Built in Number Object
Javascript Built in Math Object
Javascript Built in Date Object
Javascript Built in Arrays Object
Javascript Built in Boolean Object
Javascript Built in RegExp Object
Javascript Built in JSON Object


 
SHARE THIS IF YOU LIKE!
submit to reddit
SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.