Welcome back guys, in this module, we are going to talk about What is JavaScript, the things which make JavaScript, a powerful and popular programming language. By knowing JavaScript, you may start loving this programming language and want to start a career in the same. So, let’s begin and know what exactly is JS.
What is JavaScript?
JavaScript or JS is a High-Level & Interpreted Language because it uses an interpreter. JavaScript code is executed on the client’s web browser such as google chrome, firefox, safari, etc. so, it is also known as a client-side scripting language.
Every web browser has a JavaScript engine that executes the JavaScript code. For example, Chrome has a V8 engine and Mozilla Firefox has Spider Monkey.
It is also an untyped or dynamically typed language which means that you don’t need to declare variables along with their data types like other programming languages like C, C++, and Java which are statically typed.
In JS, you just need to write keywords either var, let, or const along with the variable name. For example –
var a; let b; const c = 10;
Here, var, let, and const are the keywords in JavaScript used to declare variables which we will learn later in the next article.
Like other programming languages, JavaScript is also a Case-Sensitive Language which means uppercase & lowercase are not the same.
It is a programming language for the web and also supports OOPs concepts i.e. classes, objects, etc. Let’s know some history of JavaScript.
History of JavaScript
JavaScript was developed by Brendan Eich in May 1995. Earlier, it was known by the name Mocha but later in 1997, it became ECMA Standard. Now, ECMAScript is the official name of JavaScript and is also maintained by them. Every year they keep on adding new features.
Why learn JavaScript?
You might be thinking about why to learn JavaScript while there are also some other popular programming languages.
- JavaScript is used as a front-end language on the Web.
- As a web developer, it is required to have an understanding of JavaScript along with HTML and CSS to make interactive web pages.
- HTML just provides structure to your web page, CSS is used to add styling to web pages, for example, adding background colour, font style, size, etc. But to add functionalities to your web page like changing HTML content or background colour on a button click then you need to use JavaScript.
- Nowadays, JS has become a powerful programming language that is also used as a Backend Language with the help of NodeJS which is built on Chrome’s V8 Engine.
- JavaScript has many libraries and frameworks used to build Web and Mobile applications.
How does JavaScript work?
As I have mentioned earlier that every web browser has JavaScript Engine to execute JavaScript Code.
A parser is used to parse the code line-by-line (because JS is an interpreted language) and checks for syntax and errors, if there exists some error then it stops parsing & throws an error otherwise it produces a data structure known as Abstract Syntax Tree (AST).
Abstract Syntax Tree translates JS code to Machine Code and finally, the code is executed on the client’s browser.
Uses of JavaScript
- Used in form validation like checking empty input field, password length, etc.
- Used in the field of Web development, Mobile Development and Desktop App Development.
- Nowadays, it is used as a server-side scripting language with the help of NodeJS.
- They are used to add functionalities to web pages.
- It is also used in Game Development.
- Used to change the appearance or presentation of a web page.
Requirements
- Basics of HTML and CSS.
- Any Code editor such as Notepad, Notepad++, VS Code, Sublime Text, etc.
- A Web Browser.
- If you know any other programming language then it is good for you but if not then you don’t need to worry. But the basics of HTML & CSS are necessary to learn JavaScript.
How to write JavaScript?
We can write JavaScript in 2 ways:
1. Internal JavaScript
JavaScript Developers write JavaScript code inside an HTML file and add JS code inside the script tag.
<!DOCTYPE html> <html> <head> <title>Document</title> </head> <body> <script> document.write('Hello'); </script> </body> </html>
2. External JavaScript
We write JavaScript code in another file and save the file with the .js extension.
We include this file inside the HTML file using the script tag.
Create two files with the names “index.js” and “index.html” and fill those files with the following content.
index.js
document.write('<h1>Hello</h1>');
index.html
<!DOCTYPE html> <html> <head> <title>Document</title> <script type="text/javascript" src='index.js'></script> </head> <body> </body> </html>
How to print “Hello, World!” in JavaScript?
1. Using document.write()
Displaying message in HTML document.
<!DOCTYPE html> <html> <head> <title>Document</title> </head> <body> <script type='text/javascript'> document.write('Hello,World!'); </body> </html>
2. Using console.log()
Prints message in the browser console.
<!DOCTYPE html> <html> <head> <title>Document</title> </head> <body> <script type='text/javascript'> console.log('Hello,World!'); </body> </html>
How to write Comments in JavaScript?
Comments in the program are used to make it readable and understandable.
It is used to explain or describe a program and is never executed by an interpreter.
Types of comments in Javascript
1. Single-line comment
Starts and ends with //.
For example,
// Printing message document.write("Hello World!");
2. Multi-line comment
Starts with /* and ends with */.
For example,
/* This is a multi-line comment. Printing message */ document.write("Hello World!");
I hope this module has helped you a lot in knowing about what is JavaScript and you might be excited to begin your career in this. For getting more modules like this, stay connected with us. Until then, Stay Happy, Stay safe and keep coding.