JavaScript
It was developed under the name Mocha, but was officially called LiveScript and renamed into JavaScript in 1995 by Brendan Eich at Netscape Communications as the scripting language for the Netscape Navigator Browser. It has been standardized by ECMA International as ECMAScript.
JavaScript + DOM/BOM + CSS + (X)HTML = DHTML.
JavaScript code in (X)HTML pages can be executed "on the fly" as the document is rendered (i.e. code outside of functions executes as the <script> element is encountered); in most cases though, JavaScript code is executed in response to document events (e.g. clicking a page element).
Versions
- JavaScript 1.5
- Jscript 5.5
- ECMAScript v3 (ECMA-262 3rd Edition)
- JavaScipt 1.8.1
- ECMAScript 5 (ECMA-262 5th Edition)
Frameworks
JavaScript can be linked or embedded in web pages using the <script> element.
- Linked
<script type="text/javascript" src="scripts.js"/></script>
<script type="text/javascript">
<!-- hide script from non-JavaScript browsers ..
/* script code here */
// end of script .. -->
</script>
Basic Language Features
- Paradigm
- Object-oriented (prototype-based)
- Functional
- Imperative Scripting Language
- Java/C like Syntax
- Implicit semicolon insertion for statement termination
- Identifiers are alphanumeric, _, and $ characters
- Single-line (//) or block (/**/) comments
- Type system and variable scoping rules
- Dynamic (aka loose or weak) typing
- Global (aka top-level) or local scopes
- Data types:
- Primitive types
- Numbers (decimal, hexadecimal notation)
- Booleans (true, false)
- Strings (Single or Double quote delimited)
- Undefined and Null
- Composite(object)types
- Core Javascript Object
- Object, Number, Boolean, String, Date, Math, Global, RegExp, Error
- Arrays
// array creation var myArr = []; var myArr = new Array(); // array methods // Appends to the end of the array can contain any object or value myArr.push('test'); myArr.push({name:'chris'}); // adding objects myArr.push(function(){ console.log('Hello'); }) // even functions // Removes and returns last item of array var item = myArr.pop(); // Appends as first item in array myArr.unshift('first item'); // Same as pop but with the first element instead of last var firstItem = myArr.shift(); var arr = ['one', 'two', 'three']; // Returns ['one', 'two'] var sliced = arr.slice(0, 2); // Reversed the ordering of an array arr.reverse(); // Sorts an array alphanumerically arr.sort(); // This also can take a callback which will be use for sorting // This sorts the array numerically arr.sort(function(a, b){ return a - b; }); // Converts an array into a string using a delimeter arr.join(','); // returns the string "one,two,three" /* Iterating an array */ // Array and for-each for(var index in arr){ console.log(arr[index]); } // Or the built in forEach method arr.forEach(function(value){ console.log(value); });
There are also the iterator methods that are used to iterate for each of the values in array without resorting to for loops
- filter(callback)
- Filters an array using the callback
- every(callback)
- Tests all elements pass the test, returns a boolean
- map(callback)
- Does the callback to all elements
- forEach(callback)
- Same as the for-each JavaScript but as a method
- reduce(callback)
- Uses an accumulator which can be used for each iteration
- Functions (Functions, Argument)
// Function declaration function sayHello(){ console.log('Hello!'); } // alternatively var sayHello = function(){ console.log('Hello!') } // Both functions can be called by invoking the name sayHello(); // Functions can also take arguments: function sayHello(name){ console.log('Hello ' + name); } // invoking would be like: sayHello('Cee'); // Using the arguments array function usingOptionalArguments(){ console.log(arguments[0]); console.log(arguments[1]); console.log(arguments[2]); } usingOptionalArguments('first', 'second', 'third'); // Function with return type function isGreater(a, b){ return a > b; } isGreater(3, 2); // returns true
- Objects
There are couple of ways of creating objects, but the common way is to use an object literal
// The best way to understand objects is by creating an object literal var buddha = { name: 'Siddharta', sex: 'Male', hometown: 'India' } // Now we have an object assigned to the variable buddha // We can access its properties by using the dot notation console.log(buddha.name); // this prints out 'Siddharta' // or console.log(buddha["name"]); // To set another property we can either do: buddha.fullName = "Siddharta Gautama"; // or buddha['fullName'] = "Siddharta Gautama" // To edit a property its going to be the same buddha.fullName = "Sidhharta Gautama - The Buddha" // The snippet below is commonly used to get properties of objects for (var propertyName in buddha){ console.log(buddha[propertyName]); } // To delete a property from that object we can do: delete buddha.fullName // Objects can also be converted to strings and vice-versa var stringRepresentation = JSON.stringify({name: 'Chris'}); // Converting it to an object again var chris = JSON.parse(stringRepresentation);
- Anchor, Applet, Attr, Comment, DOMException, DOMImplementation, DocumentFragment, Element, Event, Form, Image, Input, Layer, Link, Node, Option, Select, Style, Text, TextArea