DEV Community

Cover image for What is JavaScript?
CodeByte
CodeByte

Posted on

What is JavaScript?

INTRODUCTION

JavaScript is a popular and versatile programming language that powers the dynamic and interactive features of web pages. In this article, I will provide an overview of what JavaScript is, what it can do, and how it works.

Now what is JavaScript?

JavaScript is a scripting or programming language that allows you to implement complex features on web pages, such as displaying timely content updates, interactive maps, animated graphics, scrolling video jukeboxes, and more. It is the third layer of the standard web technologies, along with HTML and CSS.

HTML is the markup language that we use to structure and give meaning to our web content, such as defining paragraphs, headings, tables, images, and videos.

CSS is the language of style rules that we use to apply styling to our HTML content, such as setting background colors, fonts, and layouts.

JavaScript is the language that enables us to create dynamic and interactive behavior for our web pages, such as responding to user input, manipulating the HTML and CSS elements, and communicating with web servers and databases.

JavaScript is not the same as Java, which is a different programming language that is mainly used for developing desktop and mobile applications.

JavaScript was created in 1995 by Brendan Eich, who was working at Netscape, a web browser company. He originally named it Mocha, then LiveScript, and finally JavaScript, to capitalize on the popularity of Java at the time. However, the two languages have very different syntax, features, and uses.

JavaScript is an interpreted language, which means that it does not need to be compiled before running. Instead, the JavaScript code is executed by a JavaScript engine, which is a program that can understand and execute JavaScript commands.

Every modern web browser has a built-in JavaScript engine, such as V8 for Chrome, SpiderMonkey for Firefox, Chakra for Edge, and JavaScriptCore for Safari.

JavaScript is also a high-level language, which means that it abstracts away many of the low-level details of how the computer works, such as memory management, pointers, and binary operations. This makes JavaScript easier to learn and use than lower-level languages, such as C or Assembly.

JavaScript is a multi-paradigm language, which means that it supports different styles of programming, such as imperative, declarative, functional, object-oriented, and event-driven. This gives JavaScript developers a lot of flexibility and choice in how they write their code.

JavaScript is a dynamically typed language, which means that it does not enforce strict rules on the types of data that variables can hold. For example, a variable can store a number, a string, an object, or a function, and can change its type at any time. This can make JavaScript code more concise and expressive, but also more prone to errors and bugs.

What can JavaScript do?

JavaScript can perform a variety of tasks on web pages, such as:

  • Manipulating the HTML elements and CSS styles of the page.
    Responding to user actions and events, such as clicks, mouse movements, keyboard inputs, etc.

  • Validating and processing user input, such as forms, fields, and buttons.

  • Sending and receiving data from web servers, using technologies such as AJAX, JSON, and WebSockets.

  • Creating and controlling multimedia content, such as audio, video, animations, and graphics.

  • Accessing and modifying the browser features and settings, such as cookies, local storage, geolocation, notifications, etc.

  • Implementing complex algorithms and logic, such as games, simulations, and calculations.

JavaScript can also be used for purposes beyond the web browser, such as:

  • Developing desktop applications, using frameworks such as Electron, NW.js, and NodeGUI.

  • Developing mobile applications, using frameworks such as React Native, Ionic, and NativeScript.

  • Developing web servers and backend applications, using frameworks such as Node.js, Express, and Meteor.

  • Developing games and graphics, using frameworks such as Phaser, Pixi.js, and Three.js.

  • Developing data analysis and machine learning applications, using frameworks such as TensorFlow.js, D3.js, and Chart.js.

How does JavaScript work?

  • The JavaScript code is written by the developer in a text editor or an integrated development environment (IDE).

  • The JavaScript code is embedded in an HTML file, using the <script> tag, or linked to an external file, using the src attribute.

  • The HTML file is loaded by the web browser, which parses the HTML and renders the page.

  • The JavaScript engine in the browser reads and executes the JavaScript code, line by line, from top to bottom.

  • The JavaScript code interacts with the HTML document object model (DOM), which is a tree-like representation of the page elements and attributes.

  • The JavaScript code modifies the DOM, which in turn updates the page appearance and behavior.

Here is a simple example of how JavaScript works:

<html>
<head>
  <title>JavaScript Example</title>
</head>
<body>
  <h1 id="title">Hello, World!</h1>
  <button id="button">Change Color</button>
  <script>
    // Get the elements by their id
    const title = document.getElementById("title");
    const button = document.getElementById("button");

    // Define a function to change the color of the title
    function changeColor() {
      // Generate a random color
      const randomColor = Math.floor(Math.random() * 16777215).toString(16);
      // Set the color of the title to the random color
      title.style.color = "#" + randomColor;
    }

    // Add an event listener to the button
    button.addEventListener("click", changeColor);
  </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

You can try this above code below:

What is JavaScript?|Codepen

In this example, the JavaScript code does the following:

  • It gets the <h1> and <button> elements by their id using the document.getElementById() method.

  • It defines a function called changeColor() that generates a random hexadecimal color and sets the style.color property of the <h1> element to that color.

  • It adds an event listener to the <button> element using the addEventListener() method, which invokes the changeColor() function whenever the button is clicked

As a result, the page displays a heading that says โ€œHello, World!โ€ and a button that says โ€œChange Colorโ€. When the button is clicked, the color of the heading changes randomly.

Conclusion
JavaScript is a powerful and versatile programming language that can be used for a wide range of purposes, from web development to data analysis. It is one of the most popular and widely used languages in the world, and it is constantly evolving and improving. Learning JavaScript can open up many opportunities and possibilities for developers and enthusiasts alike.

Top comments (4)

Collapse
 
citronbrick profile image
CitronBrick • Edited

Brenden Eich originally wanted to put Scheme (a Lisp variant) on the browser, but was made to adopt a Java like syntax.

This is what Scheme looks like:

(define areaOfCircle(radius)
    (* 3.14 radius radius))

'using Pythagoras theorem
(define hypoteneuse(side1 side2) 
    (sqrt (+ (* side1 side1) (* side2 side2))))
Enter fullscreen mode Exit fullscreen mode

Some comments may only be visible to logged-in visitors. Sign in to view all comments.