2. ECMAScript


  • A Web browser is considered a host environment for ECMAScript, but it is not the only host environment.
  •  ECMAScript describes the following:
❑ Syntax
❑ Types 
❑ Statements 
❑ Keywords 
❑ Reserved Words 
❑ Operators 
❑ Objects
ECMAScript is simply a description, defining all the properties, methods, and objects of a scripting language. Other languages implement ECMAScript, as JavaScript does (see Figure 1-2), as the baseline for functionality.  

ECMAScript is separated into editions rather than versions because it is defined in a standard called ECMA-262. Like any standard, ECMA-262 can be edited and updated.  
Today, all popular Web browsers comply with the third edition of ECMA-262.


Syntax

  • Everything is case-sensitive. Just as with Java, variables, function names, operators, and everything else is case-sensitive, meaning that a variable named test is different from one named Test.
  • Variables are loosely typed. Unlike Java and C, variables in ECMAScript are not given a specific type. Instead, each variable is defined using the var operator and can be initialized with any value. This enables you to change the type of data a variable contains at any point in time (although you should avoid doing so whenever possible). 
             Some examples: 
                        var color = “red”;
                        var num = 25;
                        var visible = true; 
  • End-of-line semicolons are optional.
             Example: 
                        var test1 = “red”
                        var test2 = “blue”;
  • Comments are the same as in Java, C, and Perl.
           //this is a single-line comment
           /* this is a multiline comment */
  • Braces indicate code blocks.
if(condition){
              //I'm in a block
          }

Variables

var is used.
var a;
var b = 1, c =3, d = 'ffff';

//Example
var str = "I am a string";  //type of str variable is string;
str = 56;  //assigned a number to str;

alert(str); //results in "56", now its type changed from "string" to "number"! coz its loosely-typed.


  • The first character must be a letter, an underscore (_), or a dollar sign ($).
  • All remaining characters may be underscores, dollar signs, or any alphanumeric characters.
  • Camel Notation. Ex: var channelName; //I suggest the reader to prefer this notation.
  • Pascal Notation. Ex: var ChannelName;
  • Hungarian Notation. Ex: var iChannelName; //i denotes integer. we will not discuss this notation in detail.

No comments:

Post a Comment