Introduction to JavaScript

JavaScript (JS)  is the world’s most popular lightweight, interpreted compiled programming language. It is also known as a scripting language for web pages. It can be used for Client-side as well as Server-side developments. JavaScript can be added to your HTML file in two ways:

  • Internal JavaScript:  We can add JS code directly to our HTML file by writing the code inside the <script> tag. The <script> tag can either be placed inside the <head> or the <body> tag according to the requirement.
  • External JavaScript File:  We can create a file with .js extension and paste the JS code inside it. After creating the file, add this file in <script src=”file_name.js”> tag inside <head> tag of the HTML file. Syntax:  It is the basic syntax to write code.
<script>
// JS Code
</script>

Example 1:  It is the basic example to embed JS code in an HTML file.

<!DOCTYPE html>
<html lang="en">
<head>
<title>
Basic Example to Describe JavaScript
</title>
</head>
<body>
<!-- JavaScript code can be embedded inside
head section or body section -->
<script>
console.log("Welcome to GeeksforGeeks");
</script>
</body>
</html>

Output:  The output will display on console.

Welcome to GeeksforGeeks

Example 2: This example describes a simple function and prints the values.

JavaScript

<script>

// Declare a variable and initialize it
// Global variable declaration
var Name = "Apple";

// Function definition
function MyFunction() {

// Local variable declaration
var num = 45;

// Console value of Global variable
console.log(Name);

// Console value of local variable
console.log("\n" + num);
}

// Function call
MyFunction();
</script>

Output:  Console output

Apple
45

Why JavaScript is used?

JavaScript is the most popular programming language for both client-side and server-side to make interactive web pages. It is mainly used to develop websites and web-based applications.

  • Creating Interactive Websites:  JavaScript is used to make the web pages dynamic and interactive. It means using JavaScript, we can change the web page content and styles dynamically.
  • Building Applications:  JavaScript is used to make web and mobile applications. To build the web and mobile apps, we can use most popular JavaScript frameworks like – ReactJS, React Native, Node.js etc.
  • Web Servers:  We can make robust server applications using JavaScript. To be precise we use JavaScript frameworks like Node.js and Express.js to build these servers.
  • Game Development:  JavaSCript can be used to design Browser games. In JavaScript, lots of game engines available that provide frameworks for building games.

How JavaScript is different from HTML?

  • JavaScript is an advanced programming language that makes web pages more interactive and dynamic whereas HTML is a standard markup language that provides the primary structure of a website.
  • JavaScript simply adds dynamic content to websites to make them look good and HTML work on the look of the website without the interactive effects and all.
  • JavaScript manipulates the content to create dynamic web pages whereas HTML pages are static which means the content cannot be changed.
  • JavaScript is not cross-browser compatible whereas HTML is cross-browser compatible.
  • JavaScript can be embedded inside HTML but HTML can not be embedded inside JavaScript.

Why to learn JavaScript ?

JavaScript is the most popular and hence the most loved language around the globe. Apart from this, there are abundant reasons to learn it. Below are a listing of few important points:

  • No need of compilers:  Since JavaScript is an interpreted language, therefore it does not need any compiler for compilations.
  • Used both Client and Server-side:  Earlier JavaScript was used to build client-side applications only, but with the evolution of its frameworks namely Node.js and Express.js, it is now widely used for building server-side applications too.
  • Helps to build a complete solution:  As we saw, JavaScript is widely used in both client and server-side applications, therefore it helps us to build an end-to-end solution to a given problem.
  • Used everywhere:  JavaScript is so loved because it can be used anywhere. It can be used to develop websites, games or mobile apps, etc.
  • Huge community support:  JavaScript has a huge community of users and mentors who love this language and take it’s legacy forward.