JavaScript Tutorial

This JavaScript tutorial has been designed for beginners as well as working professionals to help them understand the basic to advanced concepts and functionalities of JavaScript. It covers most of the important concepts related to JavaScript such as operators, control flow, functions, objects, OOPs, Asynchronous JavaScript, Events, DOM manipulation and much more.

What is JavaScript

JavaScript is a lightweight, interpreted programming language. It is commonly used to create dynamic and interactive elements in web applications. JavaScript is very easy to implement because it is integrated with HTML. It is open and cross-platform.

Why to Learn JavaScript?

JavaScript is a MUST for students and working professionals to become a great Software Engineer, especially when they are working in Web Development Domain. We will list down some of the key advantages of learning JavaScript−

  • JavaScript is the most popular programming language in the world, making it a programmer’s great choice. Once you learn JavaScript, it helps you develop great front-end and back-end software using different JavaScript based frameworks like jQuery, Node.JS, etc.
  • JavaScript is everywhere, it comes installed on every modern web browser and so to learn JavaScript, you really do not need any special environment setup. For example, Chrome, Mozilla Firefox, Safari, and every browser you know as of today, supports JavaScript.
  • JavaScript helps you create really beautiful and crazy fast websites. You can develop your website with a console like look and feel and give your users the best Graphical User Experience.
  • JavaScript usage has now extended to mobile app development, desktop app development, and game development. This opens many opportunities for you as JavaScript Programmer.
  • Due to high demand, there is tons of job growth and high pay for those who know JavaScript. You can navigate over to different job sites to see what having JavaScript skills looks like in the job market.
  • Great thing about JavaScript is that you will find tons of frameworks and Libraries already developed which can be used directly in your software development to reduce your time to market.
  • JavaScript is in all over the world, and companies like Google, Meta, Microsoft, PayPal, LinkedIn, etc. also use JavaScript.
  • Furthermore, JavaScript has more than 1.5 lakh libraries. It is also growing.
  • A huge community of JavaScript is available on the internet with students, developers, and mentors. So anyone can easily get support.

There could be 1000s of good reasons to learn JavaScript Programming. But one thing for sure, to learn any programming language, not only JavaScript, you just need to code, and code, and finally code until you become an expert.

JavaScript Applications

As mentioned before, JavaScript is one of the most widely used programming languages(Front-end as well as Back-end). It has its presence in almost every area of software development. I’m going to list few of them here:

  • Client side validation − This is really important to verify any user input before submitting it to the server and JavaScript plays an important role in validating those inputs at front-end itself.
  • Manipulating HTML Pages − JavaScript helps in manipulating HTML page on the fly. This helps in adding and deleting any HTML tag very easily using JavaScript and modify your HTML to change its look and feel based on different devices and requirements.
  • User Notifications − You can use JavaScript to raise dynamic pop-ups on the webpages to give different types of notifications to your website visitors.
  • Back-end Data Loading − JavaScript provides Ajax library which helps in loading back-end data while you are doing some other processing. This really gives an amazing experience to your website visitors.
  • Presentations − JavaScript also provides the facility of creating presentations which gives website look and feel. JavaScript provides RevealJS and BespokeJS libraries to build a web-based slide presentation.
  • Server Applications − Node JS is built on Chrome’s JavaScript runtime for building fast and scalable network applications. This is an event based library which helps in developing very sophisticated server applications including Web Servers.
  • Machine learning − Developers can use the ML5.js library to complete the task related to machine learning.
  • Game Developments − JavaScript contains multiple libraries and NPM packages to design graphics for the game. We can use HTML, CSS and JavaScript with libraries to develop the games.
  • Mobile applications − We can use frameworks like React Native to build feature-rich mobile applications.
  • Internet of Things (IoT) − JavaScript is used to add functionality to devices like smartwatches, earbuds, etc.
  • Data visualization − JavaScript contains the libraries like D3.js to visualize the data efficiently. The D3.js is also used to prepare high-quality charts to visualize the data.
  • Cloud computing − We can use JavaScript in serverless computing platforms like Cloudflare and AWS lambda to write and deploy functions on the cloud.

This list goes on, there are various areas where millions of software developers are happily using JavaScript to develop great websites and other software.

Basic JavaScript Example

class Car{
constructor(name){
this.brand = name;
}
make(){
return ‘My card brand is ‘ + this.brand;
}
}

mycar = new Car(“Suzuki”);
console.log(mycar.make());

Who Should Learn JavaScript?

This JavaScript tutorial has been prepared for student as well as working professionals to help them understand the basic functionality of JavaScript to build dynamic web pages and web applications.

JavaScript – Roadmap

This Roadmap will guide you to master JavaScript. You will learn core concepts, advanced techniques, and best practices by following mentioned topics. This step-by-step path will help you as a beginner.

What is a Tutorial Roadmap?

Tutorial Roadmap typically covers the journey from beginner to advanced user, including key concepts, practical applications, and best practices.

JavaScript Roadmap

Master JavaScript from beginner to expert with our comprehensive 2024 roadmap. Learn about essential DOM manipulation, async programming, and best practices. Includes practical examples, learning resources, and a structured timeline for becoming a HTML expert.

How JavaScript Roadmap Can help you?

This JavaScript roadmap includes essential programming concepts, hands-on coding exercises, and valuable learning resources. Calculate your learning timeline, implement strategic coding practices, and build a solid foundation in JavaScript development with our clear, structured approach.

 

Javascript – Overview

What is JavaScript?

JavaScript is a dynamic computer programming language. It is lightweight and most commonly used as a part of web pages, whose implementations allow client-side script to interact with the user and make dynamic pages. It is an interpreted programming language with object-oriented capabilities.

JavaScript is a single-threaded programming language that we can use for client-side or server-side development. It is a dynamically typed programming language, which means that we dont care about variable data types while writing the JavaScript code. Also, it contains the control statements, operators, and objects like Array, Math, Data, etc.

JavaScript was first known as LiveScript, but Netscape changed its name to JavaScript, possibly because of the excitement being generated by Java. JavaScript made its first appearance in Netscape 2.0 in 1995 with the name LiveScript. The general-purpose core of the language has been embedded in Netscape and other web browsers.

The ECMA-262 Specificationdefined a standard version of the core JavaScript language.

  • JavaScript is a lightweight, interpreted programming language.

  • Designed for creating network-centric applications.

  • Complementary to and integrated with Java.

  • Complementary to and integrated with HTML.

  • Open and cross-platform

History of JavaScript

JavaScript is developed by Brendan Eich, a computer scientist and programmer at Netscape Communications Corporation. The initial name of the JavaScript was the ‘Mocha’. After that, it changed to ‘LiveScript’, and then ‘JavaScript’.

Between 1996 and 1997, the European Computer Manufacturers Association (ECMA) standardized JavaScript. After that, 3 revisions of the JavaScript have been done.

In ES5 (2009), Node.js was introduced to use JavaScript as a server-side language. The ES6 (2015) was a significant revision of JavaScript, introducing advanced features into JavaScript.

Currently, JavaScript has reached the version ES14. ES14 (ECMAScript 2023) the 14th version, was released in June 2023.

Client-Side JavaScript

Client-side JavaScript is the most common form of the language. The script should be included in or referenced by an HTML document for the code to be interpreted by the browser.

It means that a web page need not be a static HTML but can include programs that interact with the user, control the browser, and dynamically create HTML content.

The JavaScript client-side mechanism provides many advantages over traditional CGI server-side scripts. For example, you might use JavaScript to check if the user has entered a valid e-mail address in a form field.

The JavaScript code is executed when the user submits the form, and only if all the entries are valid they would be submitted to the Web Server.

JavaScript can be used to trap user-initiated events such as button clicks, link navigation, and other actions that the user initiates explicitly or implicitly.

The Popular client-side libraries for JavaScript development are ReactJS, NextJS, Vue JS, Angular JS, etc.

Server-Side JavaScript

In the early days, JavaScript was used for front-end development to add behaviors to HTML pages. Since 2009, JavaScript is also used as a server-side programming language to build scalable and dynamic web applications.

Node.js is one of the best and most popular JavaScript runtime environments for building the server of applications using JavaScript. Using Node.js, we can execute the JavaScript code outside the browser and manage the server task. The server tasks can be an interaction with the database, APIS, file handling, or maybe network communication. Due to the event-driven architecture of Node.js, it is faster than other server-side programming languages.

Advantages of JavaScript

The merits of using JavaScript are −

  • Less server interaction − You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.

  • Immediate feedback to the visitors − They don’t have to wait for a page reload to see if they have forgotten to enter something.

  • Increased interactivity − You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.

  • Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.

Limitations of JavaScript

We cannot treat JavaScript as a full-fledged programming language. It lacks the following important features −

    • Client-side JavaScript does not allow the reading or writing of files. This has been kept for security reasons.

    • JavaScript cannot be used for networking applications because no such support is available.

    • JavaScript doesn’t have any multi-threading capabilities.

Once again, JavaScript is a lightweight, interpreted programming language that allows you to build interactivity into otherwise static HTML pages.

Imperative vs. Declarative JavaScript

The imperative and declarative is a programming paradigm, and JavaScript follows both.

  • Imperative JavaScript − In imperative JavaScript, we write code in the manner that the code describes the steps to get the output. So, we are concerned about the code execution flow and output both. For example, to sum all array elements, if we write code for loop, it explains each step to get the sum.

  • Declarative JavaScript − In declarative JavaScript, we don’t need to worry about execution flow, but we should get the correct output at the end. For example, we use a built-in array.reduce() method to get a sum of array elements. Here, we dont concern about how reduce() method is implemented in the library.

JavaScript Development Tools

One of the major strengths of JavaScript is that it does not require expensive development tools. You can start with a simple text editor such as Notepad. Since it is an interpreted language inside the context of a web browser, you don’t even need to buy a compiler.

Here are various free tools which can be helpful while developing applications with JavaScript.

  • Visual Studio Code (VS Code) − The VS Code is a code editor used by most developers to write JavaScript code. It is feature rich and contains various extensions that can increase the productivity of any developer.

  • Chrome dev tools − Programmers may use the Chrome dev tools to debug the JavaScript code. However, they can use the debugging tool of any browser as most browser comes with it.

The above 2 tools increase the productivity of the developer for writing the code. Furthermore, you may use other tools like Git for version controlling, Webpack to build your application, etc.

Where is JavaScript Today?

In 2015, the ES6 version of JavaScript was launched with significant enhancements, including object-oriented concepts, anonymous functions, template literals, etc. In June 2023, the ES14 (ECMAScript 2023), the 14th version of JavaScript was launched.

Javascript – Features

JavaScript Features

JavaScript is a highly popular and widely-used programming language for web development. It has a variety of features that make it powerful and flexible. Some of these features include being dynamic, lightweight, interpreted, functional, and object-oriented.

Many open-source JavaScript libraries are available, facilitating the utilization of JavaScript in both frontend as well as backend development. Let’s highlight some of the key features of JavaScript.

Easy Setup

We don’t need a particular editor to start writing the JavaScript code. Even anyone can write JavaScript code in NotePad. Also, JavaScript can be executed in the browser without any interpreter or compiler setup.

You can use the <script > tag to add JavaScript in the HTML file. However, it also allows you to add JavaScript to the web page from the external JavaScript file, having ‘.js‘ extension.

Browser Support

All browsers support JavaScript, as all modern browser comes with the built-in JavaScript execution environment.

However, you can also use the window object to check whether the browser supports JavaScript or its particular feature.

Dom Manipulation

JavaScript allows developers to manipulate the webpage elements. Also, you can control the browser.

It contains the various methods to access the DOM elements using different attributes and allows to customize the HTML elements.

 

Event Handling

JavaScript allows you to handle the events used to interact with the web page.

 

For example, you can detect the mouse click on a particular HTML element using JavaScript and interact with the HTML element.

Some other events also exist, like detecting the scrolling behavior of web page, etc. We will explore all events in the JavaScript events chapter.

Dynamic Typing

JavaScript decides the type of variables at runtime. So, we dont need to care about variable data type while writing the code, providing more flexibility to write code.

Also, you can assign the values of the different data types to a single variable. For example, if you have stored the number value of a particular variable, you can update the variables value with the string.

Functional Programming

JavaScript supports the functional programming. In JavaScript, you can define the first-class function, pure functions, closures, higher-order functions, arrow funcitons, function expresions, etc.

It mostly uses the functions as a primary building blocks to solve the problem.

 

Cross-platform Support

Each operating system and browser support JavaScript. So, it is widely used for developing websites, mobile applications, games, desktop applications, etc.

Object-oriented Programming

JavaScript contains the classes, and we can implement all object-oriented programming concepts using its functionality.

It also supports inheritance, abstraction, polymorphism, encapsulation, etc, concepts of Object-oriented programming.

Built-in Objects

JavaScript contains built-in objects like Math and Date. We can use a Math object to perform mathematical operations and a Date object to manipulate the date easily.

However, you can also manipulate the functionality of the built-in object.

Object Prototypes

In JavaScript, everything is an object. For example, array, function, number, string, boolean, set, map, etc. are objects.

Each object contains the prototype property, which is hidden. You can use the prototype property to achive inheritance or extend the functionality of class or object, by other objects functionality.

Global Object

JavaScript contains the global object to access the variables which are available everywhere.

To access global variables in the browser, you can use the window object, and in Node.js, you can use the ‘global’ keyword to access global variables.

Recently, globalThis keyword is introduced to access the global variables, and which is supported by the most runtime environments.

Built-in Methods

JavaScript also contains the built-in methods for each object. Developers can use the built-in methods to write efficient and shorter codes.

For example, the Array object contains the filter() method to filter array elements and the sort() method to sort the array. The String object contains the replace() method to replace text in the string, the trim() method to remove whitespaces from the string, etc.

Modular Programming

JavaScript allows you to write the code in different modules and connect them with the parent module. So developers can write maintainable code.

By writing the code in a separate module, you can reduce the complexity of the code and reuse each module whenever you require.

JSON

JSON stands for JavaScript object notation. It is a widely used data format to exchange data between two networks. For example, server and client.

JavaScript also supports the JSON format to store the data.

Asynchronous Programming

JavaScript is a single-threaded programming language. To execute your code faster, you can use asynchronous programming.

You can use promises in JavaScript to write asynchronous code, allowing us to handle multiple tasks asynchronously.

Event-driven Architecture

The event-driven architecture of JavaScript allows developers to create interactive and responsive web applications by handling a large user base concurrently.

Due to the vast features and applications of JavaScript, the front end of Facebook is built on JavaScript. Netflix is built using the ReactJS framework of JavaScript. Similarly, Amazon, PayPal, Airbnb, LinkedIn, Twitter, etc., are also built using JavaScript.

Server-side Support

The Node.js runtime environment of JavaScript is widely used to create the backend of the application, as javaScript can also be used to create servers. It allows you to create a scalable backend for the application.

Enabling JavaScript in Browsers

Enabling JavaScript

All modern browsers come with built-in support for JavaScript, and it has enabled JavaScript by default. Frequently, you may need to enable or disable this support manually. This chapter explains how to turn JavaScript support on and off in your browsers: Chrome, Microsoft Edge, Firefox, Safari, and Opera.

JavaScript in Chrome

Here are the steps to turn on or turn off JavaScript in Chrome −

  • Click the Chrome menu at the top right-hand corner of your browser.

  • Select the Settings option.

  • Click on the Privacy and Security tab from the left sidebar.

  • Click Show advanced settings at the end of the page.

  • Next, click on the Site Settings tab.

  • Now, scroll to the bottom of the page, and find the content section. Click on the JavaScript tab in the content section.

  • Here, you can select a radio button to turn JavaScript on or off.

Also, you can add the URLS of the custom website to block and unblock JavaScript on particular websites.

JavaScript in Microsoft Edge

Here are simple steps to turn on or turn off JavaScript in your Microsoft Edge −

  • Click Edge menu (three dots) at top right-hand corner of the edge browser.

  • Follow More Tools Internet Options from the menu.

  • Select Security tab from the dialog box.

  • Click the Custom Level button.

  • Scroll down till you find Scripting option.

  • Select Enable radio button under Active scripting.

  • Finally click OK and come out.

To disable JavaScript support in your Microsoft Edge, you need to select Disable radio button under Active scripting.

JavaScript in Firefox

Here are the steps to turn on or turn off JavaScript in Firefox −

  • Open a new tab type about: config in the address bar.

  • Then you will find the warning dialog. Select Ill be careful, I promise!

  • Then you will find the list of configure options in the browser.

  • In the search bar, type javascript.enabled.

  • There you will find the option to enable or disable javascript by right-clicking on the value of that option select toggle.

  • If javascript.enabled is true, it converts to false upon clicking toggle. If javascript is disabled, it gets enabled upon clicking toggle.

JavaScript in Safari

When you install the Safari web browser, JavaScript comes installed by default. If you have disabled it and want to enable it, follow the steps below.

  • Click on the safari menu from the top-left corner.

  • Select the preferences in the dropdown menu. It will open a new window.

  • Open the security tab.

  • Check the Enable JavaScript checkbox in the web content section to enable the javascript. You can disable the JavaScript by unchecking the checkbox.

  • Now, close the preference window and reload the web page.

JavaScript in Opera

Here are the steps to turn on or turn off JavaScript in Opera −

  • Follow Tools Preferences from the menu.

  • Select the Advanced option from the dialog box.

  • Select Content from the listed items.

  • Select Enable JavaScript checkbox.

  • Finally, click OK and come out.

To disable JavaScript support in your Opera, you should not select the enable JavaScript checkbox.

JavaScript in Brave

The Brave is well-known for its security and privacy. So, It doesnt allow us to disable the JavaScript permanently, but we can disable the JavaScript for the particular website by following the below steps.

  • Open the website URL to disable the browser for it.

  • Now, Click on the Brave Shields icon in the address bar.

  • Find the Scripts option in the Shields panel.

  • The default value of the Scripts is Allow Scripts. If you want to disable JavaScript, choose the “Block Scripts” option.

Warning for Non-JavaScript Browsers

If you have to do something important using JavaScript, then you can display a warning message to the user using <noscript> tags.