What is JavaScript?
JavaScript is the scripting language that powers modern web pages. It allows web sites to do things like track user clicks, add animation to slideshows and presentations, and interact with the user through form validation. It can also help with a variety of other tasks, including creating server-side applications and automating processes. It is a single-threaded language and supports asynchronous programming, allowing tasks to run in the background without blocking the user’s browser.
When you visit a website, the server sends a bunch of files to your computer (probably HTML and CSS). These files lay out the appearance of the website and tell you what you can do on it (click links, submit forms, etc.). Then the server stops sending files and your browser starts rendering the page. But behind the scenes, there’s a whole lot going on in terms of processing to make all that happen. That’s where javascript comes in.
The programs that drive websites are written in a language called javascript, which is a subset of the Java programming language. Unlike most other languages, javascript programs can be embedded right into the website’s HTML code and automatically executed when the page loads.
This makes it easy for a web page designer to add interactive features that can enhance the user experience and make the site more interesting. The most common use for javascript is for field validation in web forms. For example, it can check that a user’s age entered in a form is within the range accepted by the web site. It can also be used to intuitively update content in a presentation or slideshow, such as moving to the next slide in a carousel or showing different images in a gallery.
The syntax of javascript is fairly simple. It is a functional language, which means that functions are first-class citizens and can be stored in variables, passed to other functions, and returned from them. It is also a dynamic language, meaning that you don’t have to declare a variable’s type ahead of time and that its type can change at runtime. It has a loosely-typed object model, allowing objects to inherit properties from other classes and to extend their methods through prototype chains. It also supports asynchronous programming and event delegation.
In the above code, lines 14 – 16 define the function that will calculate the square root of the number entered by the user in line 17. Line 18 renders an input box for the user to enter the number and line 19 renders the button the user will click to get the answer. Line 27 calls the pre-written function to execute its code and lines 28 – 29 display the result.
In JavaScript, variables and functions can be declared const or let. Const declarations prevent reassignments but do not prevent mutations of the variable’s value (or, for objects, its prototype chain). let declarations allow reassignments and mutations to occur in the same scope but not in other scopes. Private property names are created by appending the hash symbol # to the end of a class name. In JavaScript, these names are visible only inside the class body, so they cannot be read outside of the class (even by child classes).