Home » JavaScript » JavaScript tutorial

JavaScript Tutorial: Basics to Advance

JavaScript tutorial

Javascript Tutorial: JavaScript(JS for short) is a widely used client-side scripting language, i.e., it gets executed within the client's(user) browser. It creates interactive webpages and adds dynamic effects to them.

JavaScript is an object-oriented language, i.e., it supports all object-oriented concepts like c++ or java. However, JavaScript is not related to the Java language in any way. So in this JS tutorial, we will cover all the basics and some advanced topics of JavaScript.


History of JavaScript

  • JavaScript was initially introduced as 'LiveScript' by Netscape in 1995. It was renamed 'JavaScript' later.
  • In 1995, Netscape competed with Microsoft. That is why they approached Sun Microsystems, which was about to launch a new programming language JAVA. Java was a programming language that had all the features of Object-Oriented Programming.
  • Java was used for system programming and applet development. However, there was still a need for a scripting language for websites to enhance the user’s interactivity. Netscape, in collaboration with Sun Microsystems, developed LiveScript for its browser to compete with Internet Explorer of Microsoft..
  • Later on, LiveScript’s name has been changed to JavaScript because of Java’s popularity. The first browser which could support JavaScript was Netscape 2.0.

JavaScript engine

JavaScript Engine is a Software that is used to execute JavaScript. Earlier engines used to interpret the script but, the modern ones implement just-in-time compilation. Following steps are performed by a JavaScript Engine:

  • It reads the script (Parsing).
  • Then, it converts or compiles the script to machine language.
  • Execution of the script by the machine.

Every browser has its JavaScript engine like Chrome has V8, Firefox has SpiderMonkey, and Safari has JavaScriptCore. All the engines do the same job.


Application of JavaScript

Here is a small list of some applications of JavaScript, which we have covered in this tutorial-

  • Client-side form validation.
  • Performing Event-based actions.
  • Dynamic drop-down menus.
  • Displaying date and time.
  • Displaying popup windows and dialog boxes (like an alert box, confirm box and prompt box).
  • Adding, removing, or hiding elements from the webpage.

Features of JavaScript

Learning JavaScript is fun. It offers many features that a developer can use to make its website more appealing to the users. Some features of JavaScript are:

  • JS allows Dynamic Typing; i.e., a variable's type is not needed to be declared.
  • It is a case-sensitive language.
  • It can react to user operations to increase interactivity, like a mouse click or keypress.
  • It can perform form validation before submitting the data to the server.
  • It can set cookies in the browser.
  • Supports the storage of data in the local storage.
  • JavaScript is lightweight and platform-independent.
  • It has inbuilt functions to perform different operations.
  • It supports Object Orientation but does not use classes.

Advantages of JavaScript

There are so many advantages of JavaScript, but we will discuss the most important ones in this JavaScript tutorial:

  • Easy to Learn: It has a simple syntax and predefined methods, which helps in easy implementation of JavaScript. Although the advanced part of JavaScript requires a bit more effort.
  • Less server interaction: JS does client-side validation, which means it validates the data before sending it to the server. Hence, the user interacts with the server only when all the data gets validated. It saves server traffic, which means less load on the server.
  • Creates interactive pages: It creates interactive pages that react to the user’s operations. For example, an event gets triggered when the user hovers over any page element or pressing a keyboard key.
  • Interactive components: JavaScript includes drag-and-drop components and sliders, which provide a rich interface to the users.
  • User Notification: JavaScript can give different types of notifications to the user by its dynamic pop-ups.
  • Develop Server Applications: A framework of JavaScript called NodeJS can develop server-side applications. It has been built on Chrome's JavaScript runtime for building fast and scalable network applications.
  • Responsive Design: JS can also make a website responsive to different screen sizes of different devices. However, it is more comfortable with the help of predefined JavaScript based frameworks like Bootstrap and only CSS based PureCSS.
  • Back-end Data Loading: The AJAX (Asynchronous JavaScript) Library of JavaScript is a handy tool to load the back-end data while doing some other work on the page. With this library's help, the web page can refresh some parts of it without reloading the whole page.

Limitations of JavaScript

  • In-browser JavaScript does not copy, execute, or read/write files on the hard disk. Any access to the Operating System function is also not allowed.
  • JavaScript does not support network applications, so it cannot be used with them.
  • It does not support multi-threading and does not have multiprocessor capabilities.

What makes JavaScript unique?

We all know that JavaScript is an Object-Oriented Language. In OOP, objects encapsulate data and methods together, defined by a hierarchy of abstract classes. Classes are necessary for the Object Orientation Language. However, JavaScript does not have them even after being an OOP supporting language.

During developing the JavaScript, Brendon Eich removed the classes. He developed a new style of OOPs in which, Objects could be created and linked to other objects without having any need to define a class. Now, JavaScript has the Capabilities of OOP, but it is free from the complexity of classes.


Why learn JavaScript?

It is essential to learn JavaScript for aspiring designers and developers. It is the most powerful tool through which they can make very bold and attractive websites. We do not need to convince you to learn JavaScript as it is a necessity for Web Designers. We have listed some points to tell you the importance of JS in today's world:

  • JavaScript is the most popular scripting language in the world. All web programmers know about it and use it in their projects.
  • JavaScript helps develop front-end and back-end software and applications with different frameworks like jQuery, AngularJs, ReactJs, NodeJs, and many more.
  • The frameworks of JavaScript have tons of predefined libraries, which helps in the project's rapid development. It helps the programmers finish quickly, implement things, see the results, and launch the product.
  • The newer versions of JavaScript support mobile applications, game development, and desktop application development as well. As it widens its capabilities, opportunities for developers in various fields has also been opened up.
  • The job market is filled with JavaScript related works. The demand for JavaScript Developers is high. As a result, the payouts are also sufficient, according to someone's skills and experience. Search for it in any job portal, and you will understand the situation.

JavaScript Example - First JS program

After all this theoretical knowledge, let start this JavaScript tutorial and see the first JavaScript Example. Yes, our favorite, The “Hello World” program.

Look at the <script> tag to define the type of scripting we are about to use within the document. We are using JavaScript, so the language type is also the same. The script contains only one line to print 'Hello World' on the screen.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript </title>
</head>
<body>
<h2>Welcome to JavaScript</h2>
<script> document.write("Hello JavaScript"); </script>
</body>
</html>

Output

JavaScript

Welcome to JavaScript


Explanation:

  • The <script> tag indicates the implementation of JavaScript code within the Webpage.
  • The text/javascript is the content type. It provides information to the browser that the script being used is 'JavaScript'.
  • The document.write() function is used to display dynamic content through JavaScript. We will learn more about the document object model later.
Tip : The type attribute for <script> tag (i.e.<script type="text/javascript">is no longer required in HTML5. JavaScript is the default scripting language for HTML5.


We hope that this introduction to JavaScript was good enough for you to continue this JavaScript tutorial. Click on next to start learning the basic concepts of JavaScript.