An Introduction to JavaScript

By | June 24, 2023

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.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.