What is JavaScript

JavaScript is a programming language that adds interactivity to your website. This happens in games, in the behavior of responses when buttons are pressed or with data entry on forms; with dynamic styling; with animation, etc. This article helps you get started with JavaScript and furthers your understanding of what is possible.

JavaScript is a powerful programming language that can add interactivity to a website. It was invented by Brendan Eich.

JavaScript is versatile and beginner-friendly. With more experience, you'll be able to create games, animated 2D and 3D graphics, comprehensive database-driven apps, and much more!

JavaScript itself is relatively compact, yet very flexible. Developers have written a variety of tools on top of the core JavaScript language, unlocking a vast amount of functionality with minimum effort. These include:

Hello World

JavaScript is one of the most popular modern web technologies! As your JavaScript skills grow, your websites will enter a new dimension of power and creativity.

However, getting comfortable with JavaScript is more challenging than getting comfortable with HTML and CSS. You may have to start small, and progress gradually. To begin, let's examine how to add JavaScript to your page for creating a Hello world!

  1. Go to your test site and create a new folder named Within the scripts folder, create a new text document called main.js, and save it.
  2. In your index.html file, enter this code on a new line, just before the closing </body> tag:
  3. <script src="scripts/main.js"></script>
  4. This is doing the same job as the <link> element for CSS. It applies the JavaScript to the page, so it can have an effect on the HTML (along with the CSS, and anything else on the page).
  5. const myHeading = document.querySelector("h1"); myHeading.textContent = "Hello world!";
  6. Make sure the HTML and JavaScript files are saved. Then load index.html in your browser. You should see something like this:
What Happened

The heading text changed to Hello world! using JavaScript. You did this by using a function called querySelector() to grab a reference to your heading, and then store it in a variable called myHeading. This is similar to what we did using CSS selectors. When you want to do something to an element, you need to select it first.

Following that, the code set the value of the myHeading variable's textContent property (which represents the content of the heading) to Hello world!.

Variables

Variables are containers that store values. You start by declaring a variable with the let keyword, followed by the name you give to the variable: let myVariable;

A semicolon at the end of a line indicates where a statement ends. It is only required when you need to separate statements on a single line. However, some people believe it's good practice to have semicolons at the end of each statement. There are other rules for when you should and shouldn't use semicolons.

JavaScript is case sensitive. This means myVariable is not the same as myvariable. If you have problems in your code, check the case!

Comments

Comments are snippets of text that can be added along with code. The browser ignores text marked as comments. You can write comments in JavaScript just as you can in CSS:

/* Everything in between is a comment. */

f your comment contains no line breaks, it's an option to put it behind two slashes like this: // This is a comment