Errors in Javascript

Well, Errors are part and parcel of development. Its bound to happen no matter what precautions we take. When we are executing a JavaScript code, we may encounter variety of errors.

Why does an error occur?

There are many reasons for the occurrence of error. The most common reasons are:

  • Passing erroneous input
  • Typing mistake
  • Coding errors
  • No error handling mechanism

How does Error work in JavaScript?

The Error constructor in JavaScript will create an  Error object. When runtime error occur instance of the error object will be created by the JavaScript and will be thrown by the browser.

The Error object provides us with information about the error. The error object consists of two properties:

{} name: The name of the error

{} message: Description of the error

What are the different types of error?

The name property of the error object can have 5 possible values. Henceforth, In JavaScript there are 5 types of error:

{} TypeError

{} ReferenceError

{} SyntaxError

{} RangeError

{} URIError

</> TypeError

JavaScript is a loosely typed language. Even though JavaScript employs loose typing it detects all the standard datatypes such as string, number, date, boolean, object, array through auto type inference mechanism.

TypeError occurs when we try to assign a value to a variable or a parameter of a function which is not a valid type. The TypeError occurs when we are using a wrong type for an operation like the one below:

var playerName = "Partha";
playerName.isFinite();  // TypeError: playerName.isFinite is not a function

The above code results in TypeError  because playerName is of type string and we are trying to check if playerName is a finite number or not.

</> ReferenceError

This is one of the most common error in JavaScript. When we try to use or reference a variable that we forgot to declare, results in ReferenceError. In other words. ReferenceError occur when we try to use or reference a variable which has not been declared.

var playerScore = 340;
var highestScore = playerScore + bonusCoins; // ReferenceError: bonusCoins is not defined

In the above code we are calculating the highestScore of the player by adding up playerScore and the bonusCoins collected. Since bonusCoins is not defined, we get ReferenceError. 

</> SyntaxError

SyntaxError are pretty common during development. A SyntaxError in general is an error in the source code of the script or a program. Javascript follows a strict syntax to compile the code correctly. If any part or aspects of the code failed to conform to this syntax results in syntax error.

In short, SyntaxError is an error that occurs during the evaluation or parsing of the code.

SyntaxError can easily be identified and corrected, Thanks! to IDE’s auto code compilation(The red curved line and the red bib are the indicators for syntax error).

var playerScore = 500;
alert("Your score is "+playerScore"; //SyntaxError: missing ) after argument list

In the above code, we are showing the playerScore in an alert box. Since the closing parentheses is missing in the above. The browser throws a  SyntaxError .

</> RangeError

A range error occurs when we try to pass a numeric value that is outside the range of expected or legal values to a variable of type number.

var count = -4;
var playerName = "SomarLih";
playerName.repat(count); // RangeError: Invalid count value

So, In the above example we are trying to repeat the playerName count number of times. Since the repeat function accepts only numbers greater than 0(zero) we get RangeError. i.e. the value -4 is outside the expected range of the repeat function since the repeat function accepts only positive integers.

There is one more type of RangeError that is less commonly occurred known as “Maximum call stack size exceeded“. This error occurs when stack space is completely exhausted.

Array.apply(null, new Array(1000000000000)).map(Math.random); // RangeError: Maximum call stack size exceeded

What we are trying do here is, creating an array of 1,000,000,000,000 random numbers. Since the maximum number of arguments that can be passed to a function in JavaScript is 65,536. Each argument is allocated with a space in the stack. After 65,536th argument there will not be any stack space left hence JavaScript throws “Maximum call stack size exceeded

</> URIError

There are some restrictions imposed by the JavaScript on the characters that can encoded or decoded in the URI. URIError occurs when we try to pass invalid parameters to the encodeURI() or decodeURI() functions.

var gameUri = "http://%de.game.com/data/sam/v1/ge?scoreslength=56";
decodeURI(gameURL); // URIError: URI malformed at decodeURI

Since % is an  escape character, and we cannot encode or decode this escape character we get URIError

A Note on Error Object

The error object has two standard properties: name and message which must and will be supported by all the browsers. Apart from these standard properties browsers such as Mozilla and Microsoft Edge supports properties in the Error Object that are not been standardized yet. These properties are:

  • fileName: The name of the file in which the error has occurred. Firefox supports this property
  • lineNumber : The line number that contains the error. Firefox supports this property
  • columnNumber : The column number in a particular row or line in which the error has occurred. Firefox supports this property.
  • description : The description about the error. Supported by Microsoft
  • number : Numeric value of the error. Each error type is assigned a numeric value. Microsoft supports this.

Well, these are the different type of Errors in JavaScript. Once we know the type of the error, we can handle the error effectively. I will be soon writing a blog on handling errors.

Fin…

HTML 6 Features

Logo_html6

HTML 5 was released on 28th October 2014. HTML 5 has provided us with some exciting features  such as:
Canvas 
For drawing a 2-dimensional surface    using  JavaScript
Audio & Video
For embedding multimedia content in the web page.
WebStorageFor data storage &offline access of data.
Geolocation
For accessing the current position of the user’s device & much more.

HTML5 has provided the web developers with a great set of features and the web pages have evolved a lot soon after the introduction of HTML5. There are some features in HTML5 which are not currently present which all the web developers would expect in HTML6.
What developers expect in HTML6 is the freedom of code to build a dynamic web application not bound by the restrictions imposed by the standards and rules.

Here is my prediction on the list of features that we might possibly expect in HTML6:

</> Comprehensive Namespaces

All the HTML6 elements consist of namespaces. The main purpose of this is to avoid conflicts with other HTML elements. For example, if we want to create a title for our web page, it can be done this way in HTML6:

<html:title>My HTMl 6 title</html:title>

To create an HTML document we use:

<!DOCTYPE html>
<html:html></html:html>

To define metadata for the web page we use the <html:head> element. HTML metadata is the data about the HTML document.

This metadata is not displayed. Meta data will normally define the document title, style(s), links, scripts and also contains other meta information

<html:head>
   <!-- All meta information will be added here -->
</html:head>

 

Two of the most commonly used elements in any HTML document is the <script> and <link>.
The former is used to load external JavaScript files on to the document and the latter is used to load all the external style sheets.

In HTML5 if we were to load an script file will would use <script> element. If we were to load a stylesheet we would use <link> element.

In HTML6 this can be made simpler using just the link element and specifying the type of the file using the type attribute.

<html:head>
   <html:link src="js/mobile/bluestocks.js" type="javascript" charset="utf-8" media="mobile">
</html:head>

This element has the following attributes:

{} src: The location of the javascript file to be loaded in the document
{} type: The MIME type of the document it could be either javascript or css
{} media: The type of the device on which the script or stylesheet should be loaded.

Multimedia is an essential component of any web page.  In the current version of the HTML, we use <img> <audio> <video> to add image, audio and video respectively to our web image. This can be simplified in HTML6 by just using a single element: <media> element.

<html:body>
   <html:media type="image" src="img/mediaImage.jpg"></html:media>
</html:body>

 

The content that needs to be shown to the user can be added to the <body> element. The <body> element defines the document body. In HTML6 we can define the body element as:

<html:html>
   <html:body>
      <!-- Content goes here ... -->
   </html:body>
</html:html>

This is an overview of the HTML document structure that we can expect in HTML6.

</> Express Elements

As a web developer, we would love to create content easily and with minimal efforts. HTML5 has provided with some vital express elements such as <header> <article> <section> <nav>.

Here is the list of express elements that we would like to have in HTML6

{} <logo>
 Using the <logo>  we add a logo to our web page easily.  This improves the readability and structure of our HTML code.

<html:body>
   <header>
      <logo>
         <html:media type="image" src="img/logo.jpg"></html:media>
      </logo>
    </header>
</html:body>

{} <calendar>
 Currently, we will have to rely on plugins for displaying the calendar on the web page. In HTML 6 we don’t need to use any external plugins we can use the built-in calendar element.

<html:body>
   <calendar>
      <year>2017></year>
      <monthJan</month>
      <day>1</day>
      <day>2</day>
      ......
   </calendar>
</html:body>

{} <sidenav>
Most of the website now have side navigation bars for easy navigation across pages. The major benefit of side navigation is that it hides the ‘not so important’ pages from the user and doesn’t make the heady messy.

In the current version of HTML, we will have to rely on external plugins or UI library to achieve this. We expect that in HTML 6 this will be a built in feature.

<html:body>
   <sidenav postion="left" transition="reveal" transition-time="0.2" >
      <nav-item>
         Address
      </nav-item>
      <nav-item>
         Donate
      </nav-item>
      <nav-item>
         Site map
      </nav-item>
   </sidenav>
</html:body>

{} <contact-form>
The contact form is present in almost all the website(currently not present in this site 😦 ). In the current version, we will have to rely on a plugin or do complex structuring for our HTML document and define complex style rules. This would take a significant amount of time. In HTML 6 we don’t need to worry about complex styling and structure.  Instead, we can just use the <contact-form> element.

<contact-form>
   <email>Enter your email</email>
   <name>Your name</name>
   <phone>Your phone</phone>
   <action-button postion="right"> Submit! </action-button>
</contact-form>

 

</> Sophisticated control Video Objects

In HTML5 we use the <video> element. This video element creates a rectangular container which packs frames from the video with controls to play, pause, Increase/Decrease volume. We can also add a subtitle to our video. But HTML6 should have better capabilities. HTML6 must incorporate callback hooks and synchronization mechanisms with the capability to add multiple subtitles and support multiple video formats(mp4, flv, MPEG-1) This will improve the video experience on the web pages.

<video src="videocontent/myvideo.mp4" download-url="https://bluestocks.wordpress.com/videos/videocontent/myvideo.mp4">
   <subtitle source="videocontent/subtitle/eng.srt" lang="en" meta="English Subtitles">
   <subtitle source="videocontent/subtitle/esp.srt" lang="esp" meta="Spanish Subtitles">
</video>

{} src: Location of the video
{} downloadurl: URL to download the video
{} source: Location of the subtitle track
{} lang: Language of the subtitle track

</>  Camera Integration

The camera is an internal part of communication these days as to traditional text or voice conversations.  Users are able to share photos,  do a video chat with their buddies, conduct meetings and many things. HTML6 will provide us with the capability to add to capture an image and also allow the user for image customisation on the fly such as cropping, effects, colour filtering etc.

</>  Contact Information Access

Sometimes sharing the contact information of a person will be tricky, You will need to copy the phone number or email from the contacts application and then paste into the target field of the target screen.  HTML6 can provide us with the capability to access all the contacts in the form a drop down list so that the user can share the contact with other easily. Any web page accessing the contact details will impose threat and should only be restricted to trusted domains or IP.

</> Compressed objects Access

HTML6 can provide us with the capability to access objects such as image, audio, video, vectors from the zip file. One big advantage is the space reduction on physical drive. This will, in turn, improve the performance of the web page.  This can be achieved using <decompress> element.

Using the decompress element we specify the source of the zip file from which the content should be accessed.

<decompress src="compress/reunionpics.zip"></decompress>

If we have to show an image on our web page and if this image is present in a zip then we can simply give the location of the image in the zip file.

<decompress src="reunionpics.zip">
   <html:media type="image" src="reunionpics.zip/photos/photo1.jpg"></html:media>
</decompress>

In this way, we don’t need to decompress the zip file, we can just give the location of the zip file in the decompress element and use the reference of it in the media element.

</> Stylesheet per device

Stylesheets play a vital role for the contents of the web page. Sometimes a style applied to a component such as a paragraph or a div may not work well on all form factors. The style may break on mobile but may work well on a desktop. HTML6 can provide us with the capability to detect the type of the device and load the stylesheet which is designated specifically to that device. Also, we can load stylesheets depending on the browsers such as Firefox, chrome or safari.

<html:html>
   <html:head>
      <html:link type="css" src="css/desktop_firefox.css" target="desktop" vendor="firefox">
      <html:link type="css" src="css/desktop_chrome.css" target="desktop" vendor="chrome">
      <html:link type="css" src="css/mobile.css" target="mobile">
   </html:head>
</html:html>

The target attribute specifies the type of the devices for the style should be applied for. The vendor attribute can indicate the browser that the style needs to be applied.

In this way, we can specify a create a separate stylesheet for mobile and desktop and target for a specific browser.

Conclusion

Well, This is my prediction on the features that we can expect in HTML6. The features of HTML6 above can lead to making simpler websites.  HTML6 is in its very early stage of development and as a matter of fact, some of the developers are not aware of all the features and capabilities of HTML5. Some of the websites are still in a transition phase to HTML5. HTML 6 will take its takes before it becomes available to the web developers. The web is an ever growing and the ever expanding world. Every day we see a lot of innovations being made and capabilities of the web are pushed beyond the limits.

If you have a feature in mind that is a must in HTML 6 mention it in the comment box below.

Lock and Out