Major features introduced in ECMAScript 5
-
- It’s okay to have trailing commas in object literals. Since beginning javascript allowed trailing commas in the array.
var obj = { 'name': 'Commands Tech', 'category': 'tech new', }
However, using trailing commas in JSON is invalid and throws the error.
var myJSON = '{"name":"CommandsTech", "city":"New York",}'; JSON.parse(myJSON); VM793:1 Uncaught SyntaxError: Unexpected token } in JSON at position 42 at JSON.parse (<anonymous>) at <anonymous>:2:6
- No reserved keywords for variable names. Reserved keywords should not be used as variable names. Example:
var NaN = 'CommandsTech'; console.log(NaN); // NaN
-
parseInt() defaults to radix 10
-
JSON.parse() & JSON.stringify()
JSON.parse(): To convert text into a javascript object
JSON.stringify(): To convert a javascript object into string -
bind()
Description: Bind method allows us to call a function with this set explicit. bind method returns a bounded function with the bound object.
this.x = 10; var moduleX = { x : 20, getX: function() { return this.x; } } var retriveX = moduleX.getX; retriveX(); var getBoundX = retriveX.bind(moduleX); getBoundX(); Output: 20
-
trim()
Description: Removes whitespace on both ends of the string and returns a string
var name = ' commandsTech '; name.trim(); "commandsTech"
-
every()
Description: method takes a function as input parameter and checks if each element in an array passes the test and return a boolean
var bills = [132, 233, 916, 140]; function checkBills(bill) { return bill >= 100; } bills.every(checkBills); // Output true
-
filter()
Description: method creates a new array with all elements that pass a test implemented by the provided function
-
map()
Description: method creates a new array with the result of calling a provided function on each element.
- It’s okay to have trailing commas in object literals. Since beginning javascript allowed trailing commas in the array.
-
forEach()
Description: method calling a provided function on every array element.
-
reduce()
Description: reduce method results in a single value that is a result of calling a reducer function on each element.
filter, map, forEach, map method examples -
Date.now()
Description: returns the current time
-
Date.prototype.toISOString
-
new Date(string) and Date.parse(string)
-
Object.keys()
Description: Fetch all the keys() present in an object. Object.keys() method returns an array of own properties
const profile = { name: 'commandsTech', location: 'Hyd', }; console.log(Object.keys(profile)); // ['nane','location']
- Object.create()
Description: Create a new object taking the existing object’s prototypevar profile = { name: 'commandsTech' } var profile2 = Object.create(profile); profile2.name; // output: "commandsTech"
- Object.defineProperty & Object.defineProperties
Description: Using defineProperties we can modify or define properties on an object.const profile = {}; Object.defineProperties(profile, { name: { value: 'commandsTech', writable: true } }); console.log(profile.name); // expected output: commandsTech
-
Object.getOwnPropertyDescriptor()
Descriptor: Using this method we can know the description of the property specified within the object
var profile = { name:"ct", location: "hyd" } var descriptor = Object.getOwnPropertyDescriptor(profile, 'name'); console.log(descriptor.configurable); // output: true
-
Object.getOwnPropertyNames(obj)
Description: All the properties present on an object are returned
var profile = { name:"ct", location: "hyd" } Object.getOwnPropertyNames(profile); // output ['name', 'location']
-
Object.seal()
Description: the seal on object prevents adding of new properties and marks all existing as non-configurable properties. Values of a property can be changed(if writable).const profile = { name: 'commandsTech' }; Object.seal(profile);
-
Object.freeze()
Description: Object that is frozen cannot be changed. The freeze prevents adding new property & deleting the existing property. Prevents changing the enumerability, configurability or writability or changing. Changing of the prototype is also prevented on the frozen object.
const profile = { name: 'commandsTech' }; Object.freeze(profile);
-
Object.isSealed()
Description: Checks if an object is sealed or not
const profile = { name: 'commandsTech' }; console.log(Object.isSealed(profile)); //output: false Object.seal(profile); console.log(Object.isSealed(profile)); // output: true
- Object.isFrozen()
Description: Checks if an object is frozen or not
const profile = { name: 'commandsTech' }; console.log(Object.isFrozen(profile)); //output: false Object.freeze(profile); console.log(Object.isFrozen(profile)); // output: true
- Property Attributes: writeable, value, enumerable, configurable, get, set.
-
‘use strict’;
Description: Use strict indicates that javascript code should be executed in “strict mode”.
Example with use strict
<script> "use strict"; name = 'commandsTech'; // This will throws an error (name is not defined). </script>
Example without use strict
<script> name = 'commandsTech'; // This will not throw an error even though name is not defined </script>