Type, Values and Variables in Java Script

JavaScript types can be divided into two categories: primitive and object types. JS’s primitive include numbers, strings of text, and truth values (Boolean). The special JS values null and undefined are primitive values but they are not numbers, strings or Boolean.

Any JS value that is not a number, string, a Boolean, or null or undefined is an object. An object is the collection is a collection of properties where each property has a name and a value (either primitive value or an object). An ordinary JS object is an un-ordered collection of named values. The language also defined a special kind of object, known as array that represent an ordered collection of numbered values.

Numbers

Unlike many languages, JavaScript does not make a distinction between integer values and floating-point values. All numbers in JS are represented as floating-point values using 64-bit floating-point format. It can represent numbers as large as ±1.7976931348623157×10308and as small as ±5×10-324.

Text

A string is an immutable ordered sequence of 16-bit values, each of which typically represents a Unicode character-strings are JavaScript’s type of representing text. The length of a string is the number of 16-bit values it contains.

String Literals

To include a string literally in a JS code, simply enclose the characters of the string within a matched pair of single or double quotes (‘or “).

“ ” //the empty string : zero characters

‘testing’

“3.14”

‘name=”myName” ‘

 

A string literals must be written on a single line. In ECMAScript 5, however, you can break a string literal across multiple lines by ending each line but the last with a backslash (\). Neither the backslash not the line terminator that follow it are part of the string literal.

“two/nlines” // one string representing two lines written on one line

“one\        // One line string written on three lines. Supported by ECMAScript 5 only.

long\

line”

 

Variables

Before using any variable in JS it should be declared with var keyword, like this:

vari;

var sum;

or

vari, sum;

var message = “hello”;

vari=0; j=0; k=0;

Default value in a variable is undefined until it is initialized.

Variable Scope

The scope of a variable is the region of your program source code in which it is defined. A global variable has global scope; it is defined everywhere in your JS code. On the other hand, variable declared within a function are defined only within the body of the function. They are local variables and have local scope. Function parameters also count as local variables and are defined only within the body of the function.

Within the body of a function, a local variable take precedence over a global variable with same name.

var scope = “global”;             // declare a global variable
functioncheckscope() {
var scope = “local”;          // declaring local variable with the same name
return scope;                     // returns the local variable not global one
}
checkscope();              // output is: local

 

By | 2015-04-16T03:07:50+00:00 April 16th, 2015|JavaScript|2 Comments

About the Author:

2 Comments

  1. kishore November 25, 2014 at 3:42 am - Reply

    public class Static3
    {
    static
    {
    System.out.println(“Static Block started”);
    //System.out.println(pi);
    printpi();
    System.out.println(“Static block end”);
    }
    public static float pi=3.14f;
    public static void printpi()
    {
    System.out.println(“pi Value=”+pi);
    }

    public static void main(String[] args) {

    System.out.println(“+++++++++++++++++”);
    System.out.println(“Main()Started”);
    System.out.println(pi);
    printpi();
    System.out.println(“Main() end”);
    }

    }

    can any one suggest me about the logic what internally happens in JVM
    i got output as
    Static Block started
    pi Value=0.0
    Static block end
    +++++++++++++++++
    Main()Started
    3.14
    pi Value=3.14
    Main() end

    • Gaurav Singh December 3, 2014 at 12:25 pm - Reply

      Hi Kishore, in Java all the static elements are executed before main() that’s why static defined elements ( blocks, variables ) showed the output first and then main() came into execution.

Leave A Comment