An Introduction to JavaScript

Last Updated :

JavaScript was initially created to “make web pages alive”. These JavaScript program are called as scripts. These can be written in HTML of web page and will simply run when the page loads. Scripts don’t complile.

JavaScript is different from another language called Java.
Initially, it was called “LiveScript.” Since Java was very popular at that time, it was decided to position the new language as a “younger brother” of Java. So, renamed it as “JavaScript”. But it has no relation to Java at all.

To run JavaScript programs, you need a special program called the JavaScript engine. So, JavaScript can execute in the browser, also on the server, or actually on any device with the help of JavaScript engine.

There is embedded engine called “JavaScript virtual machine” in every browser.

  • Different engines have different names. For example:
  • V8 in Chrome, Opera, and Edge
  • SpiderMonkey in Firefox
  • Chakra in IE
  • JavaScriptCore, Nitro, and SquirrelFish in Safari

You need to remember these codenames because we will have to use them and different features are supported by different codenames.

How do engines work?
Engines are embedded in browser. Engines are complicated. But the basics are easy.

  1. The engine parses (“reads”) the script.
  2. Then it compiles (“converts”) the script to machine code.
  3. And then the machine code runs, pretty fast.

The engine applies optimizations at each step of the process. It even watches the compiled script as it runs, analyzes the data that flows through it, and further optimizes the machine code based on that knowledge.

What can JavaScript do in web browser?
Modern JavaScript is considered a “safe” programming language. Since it was initially created for browsers which does not require low-level access to memory or the CPU. So it does not provide.

The capabilities of JavaScript depend on environment in which it is running. In a web browser, JavaScript can do everythings related to manipulating webpages, interacting with users, and communicating with webservers.

  • JavaScript can do in web browser:
  • Manipulate webpages and interact with users.
  • Add new HTML, change existing content, and modify styles.
  • React to user actions like clicks, movements, and key presses.
  • Send requests to remote servers and download/upload files.
  • Handle cookies, ask questions, and display messages.
  • Store data on the client-side using “local storage.”

What cannot JavaScript do in web browser?
In-browser JavaScript has limitations to protect user safety. It cannot read/write arbitrary files, copy them, or execute programs.

  • Access to files is limited and requires user actions like dropping a file or selecting it via an <input> tag.
  • Interacting with devices like cameras and microphones requires explicit permission.
  • Different tabs/windows generally do not have knowledge of each other due to the “Same Origin Policy.”
  • JavaScript from one page cannot access another page from a different site. This limitation ensures that one page cannot steal information from another.

JavaScript can communicate with the server where the current page came from, but receiving data from other sites requires explicit agreement. This agreement is expressed in HTTP headers to ensure safety.

Note that if JavaScript used outside the browser, such as on the server, has no such limitation. Modern browsers also allow plugins/extensions that may ask for extended permissions.

JavaScript has with HTML/CSS, Supported by all major browsers and enabled by default. So, it is simple and unique. JavaScript can be used to create servers, mobile applications, etc.

Other languages:
Projects and requirements are different for everyone. JavaScript’s syntax doesn’t suit everyone’s needs. Different people want different features.

Therefore, a lot of new languages have appeared, which are converted to JavaScript before running in the browser. Transpilation allows developers to code in another language and automatically convert it “under the hood”. Examples of such languages: CoffeeScript, TypeScript, Flow, Dart, Brython, Kotlin, etc.

There are others too. Of course, even if we use one of these transpiled languages, we should also know JavaScript to really understand what we’re doing.

Comment
Next Article
Mithlesh Upadhyay Published 24 Jun, 2023 · 4 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • Strict mode in JavaScript

    For a long time, JavaScript had no compatibility issues because in every new version release old functionality were not changing. But it was not good as if there…

    2 min read
  • Code structure in JavaScript: Semicolon and Comment

    Semicolons and Comments are buidling blocks of code. Semicolons We can have as many statements in our code as we want. Statements can be separated with a semicolon.…

    3 min read
  • Hello, world! in JavaScript

    In this part of the tutorial, we will learn on core JavaScript. We need working environment o run our scripts. We will use either IDE or browser to…

    4 min read
  • Developer console in web browsers

    Code is prone to errors, and as a human, it’s natural to make mistakes. In the browser, errors are not visible to users by default, which makes it…

    2 min read
  • JavaScript Manuals and specifications

    This is a tutorial designed to help you learn the language gradually, but you’ll eventually need additional resources. 1. Specification The ECMA-262 specification is the most detailed and…

    2 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read