JavaScript
One

Web page

HTML

Content and Structure

CSS

Presentation

JavaScript

Behavior
          
            <html>
              <head>
              ...
              </head>
  <body>   ...   </body> </html>
          
          body {
            font-size: 42px;
            font-weight: bold;
            margin: 10px;
            padding: 10px;
            background-color: red;
          }
          
          

JavaScript
  • a client side langauage
  • runs inside a web browser
  • web browsers have a JavaScript engine inside them
  • cannot write a desktop application in JavaScript
  • JavaScript syntax is more like C
  • now also used on server side and in desktop applications
  • Node.js is an open-source, cross-platform runtime environment for developing server-side web applications

Data Types
  • contains a small set of data types
  • three primitive types boolean, number, and string
  • special values null and undefined
  • everything else is variations of the object type
  • number is 64-bit floating point
  • no integer type
  • number also includes the special values NaN (not a number) and Infinity

Strings
  • string is a sequence of zero or more Unicode characters
  • no separate character type
  • character is represented as a string of length 1
  • strings are quoted using the ' (single quote) or " (double quote) characters
  • quote characters can be used interchangeably
  • \ is the escape character
  • strings are immutable

Objects
  • Object is a container of name/value pairs
  • objects and hashtables are the same thing
  • names are strings
  • values can be any of the data types, including other objects
  • var myObject = {};
    var myObject = new Object();
  • JavaScript is loosely typed, so we don't use type names in declarations
  • the langauage is not "untyped", but it's loose that any type can be used anywhere

Objects contd.
  • Object description is a set of comma-separated name/value pairs inside curly braces
  • var myObject = {name: "Noname", age: 7, class: 2, section: 'C'};
  • two ways to to add, replace, or retrieve elements: subscript notation and dot notation
  • myObject["name"] = "Noname";
    myObject.name = "Noname";
  • objects can easily be nested inside of other objects
  • expressions can reach into the inner objects
  • myObject.name.firstname.length
  • new members can be added to any object at any time by assignment
  • myObject.nickname = 'Pappu';

Arrays
  • are also hashtable objects
  • no need to declare the size while constructing
  • grows automatically
  • values are located by a key, not by an offset
  • two ways to make a new array:
  • var myArray = [];
    var myArray = new Array();

Arrays contd.
  • are not typed
  • can contain numbers, strings, booleans, objects, functions, and arrays
  • can mix strings and numbers and objects in the same array
  • arrays literal notation
  • letters = ['a', 'b', 'c', 'd', 'e'];
    emptyArray = [];
    numArr = [3, 8, 11, 72, 7, 19, 21];

Functions
  • a function is an object
  • /* Declare the function 'myFunction' */
    function myFunction(myObject) {
    myObject.name = "NewName";
    }
  • can contain members just as other objects
  • always returns a value
  • default return value is undefined
  • function allows an object to act as a class, containing a constructor and a set of related methods
  • when a function is a member of an object, it is called a method
  • a special variable, called this is set to the object when we call a method of the object

Variables
  • defined with the var statement
  • if the var statement appears outside of any function, then members are added to the Global Object
  • if it appears inside a function, the scope is local to the function
  • vars which are not explicitly initialized are given the value undefined
  • vars are not typed
  • a new set of vars is made every time the function is called

Operators
  • most of the operators in JavaScript works the same way as in other languages
  • but, some have a few differences
  • arithmetic + - * / %
  • comparision == != < > <= >=
  • logical && || !
  • bitwise & | ^ >> >>> <<
  • ternary ?:

+ Operator
  • the + operator is used for both addition and concatenation
  • if any of the operands is a string, it concatenates otherwise adds
  • + unary operator can convert strings to numbers
  • +"42" = 42

==, !=, ===, !== Operators
  • == and != can do type coercion
  • === and !== do not do type coercion these are better

Logical Operators
  • && and
  • expr1 && expr2
    Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands can be converted to true; otherwise, returns false
  • || or
  • expr1 || expr2
    returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand can be converted to true; if both can be converted to false, returns false
  • ! not
  • !expr
    returns false if its single operand can be converted to true; otherwise, returns true
  • expressions that can be converted to false are:
  • null, NaN, 0, "", undefined
  • all other values (including all objects) are true

Identifiers
  • starts with a letter or _ or $
  • followed by zero or more letters, digits, _ or $
  • by convention, all variables, parameters, members, and function names start with lower case
  • except for constructors, which start with upper case
  • Comments
  • single line //
  • multi-line /* */