JavaScript Datatype

JavaScript Datatype

One of 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.

JavaScript allows you to work with three primitive data types −

  • Numbers, 123, 120.50 etc.
  • Stringsof text e.g. “This text string” etc.
  • Booleang. 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 supports a composite data type known as object. We will cover objects in detail in a separate chapter.

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 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 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 at a later point in time when you need that variable.

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

<script type=”text/javascript”>

<!–

var name = “Ali”;

var money;

money = 2000.50;

//–>

</script>

Note − Use the var keyword only for declaration or initialization, once for the life of any 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.

Related Posts

  • 58
    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. Global Variables− A global variable has global scope which means it can be defined anywhere in your JavaScript code. Local Variables− A local variable will…
    Tags: variable, javascript, variables
  • 54
    The typeof operator is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand. The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean…
    Tags: type, values, var, javascript
  • 44
    This section will provide you with the basics of what JavaScript is, and why you should use it. Objectives     JavaScript versus JAVA     Interpreted programs versus Compiled programs     Why JavaScript     What you can use JavaScript for     About JavaScript JavaScript is a lightweight, interpreted programming language. It…
    Tags: javascript
  • 44
    Let us take a sample example to print out "Hello World". We added an optional HTML comment that surrounds our JavaScript code. This is to save our code from a browser that does not support JavaScript. The comment ends with a "//-->". Here "//" signifies a comment in JavaScript, so…
    Tags: javascript
  • 42
    Tags: javascript

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: