w3resource

JavaScript Tutorial

What is a Script ?

A script is an executable list of commands like macro or batch file created by a scripting language. Scripts (like PHP, Perl) which are executed on a web server are called server-side scripts and scripts (like JavaScript) which are executed on user's computer, interpreted by the browser is called client-side scripts.

javascript logo

What is JavaScript ?

JavaScript is a cross-platform, object-oriented scripting language developed by Netscape. JavaScript was created by Netscape programmer Brendan Eich.

It was first released under the name of LiveScript as part of Netscape Navigator 2.0 in September 1995. It was renamed JavaScript on December 4, 1995. As JavaScript works on the client side, It is mostly used for client-side web development.

JavaScript is designed for use on web pages and closely integrated with HTML. JavaScript can create applications which run in the browsers such as IE, Opera, FireFox, Google Chrome and other. Netscape submitted JavaScript to ECMA International for standardization resulting in the standardized version named ECMAScript.

JavaScript and Java

JavaScript and Java are similar in some ways but fundamentally they are different. Java is a programming language developed by Sun Microsystems, Inc. and JavaScript is a scripting language developed by Netscape. Java is a server-side and static type language. JavaScript is a client-side, dynamically typed language. Java programs are compiled on the server and run on almost every platform without distribution of source code whereas scripts written in JavaScript are placed inside a HTML document and interpreted by the browser. The syntax, reserved-words of JavaScript and Java are also different.

JavaScript compares to Java

JavaScript Java
JavaScript is used for front-end web development (for example field level validation in a HTML form). Java is used as a back-end language within a web environment.
Interpreted (not compiled) by the client. Compiled bytecodes downloaded from the server, executed on the client.
Object-oriented. No distinction between types of objects. Inheritance is through the prototype mechanism, and properties and methods can be added to any object dynamically. Class-based. Objects are divided into classes and instances with all inheritance through the class hierarchy. Classes and instances cannot have properties or methods added dynamically.
Variable data types are not declared (loose typing). Variable data types must be declared as Java maintains strong type checking.
Cannot automatically write to hard disk. Cannot automatically write to hard disk.

Features of the w3resource JavaScript Tutorial

In this series of tutorials, we have covered JavaScript 1.5+ in detail. While creating this, we have taken care that learners can master the basics of JavaScript.

Here is a list of features we have included in all of the chapters :

  • We have started with a clear and simple description.
  • We have given a Syntax/Usage so that you can remember how to write it.
  • Example(s) to show how the associated concept is implemented.
  • We have shown the Output of the usage.
  • View the example in a browser.
  • Exercise to practice with our Online Practice Editor.
  • Pictorial presentation to help you to understand the concept better.
  • Since the browser is an issue while executing JavaScript, we have shown which browser support which script.

Next: JavaScript Overview



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

Memoized function

const add = () => {
  const cache = {};
  return num => {
    if (num in cache) {
      return 'From cache! ${cache[num]}';
    } else {
      const result = num + 10;
      cache[num] = result;
      return 'Calculated! ${result}';
    }
  };
};

const addFunction = add();
console.log(addFunction(10));
console.log(addFunction(10));
console.log(addFunction(5 * 2));

The add function is a memoized function. With memoization, we can cache the results of a function in order to speed up its execution. In this case, we create a cache object that stores the previously returned values.
If we call the addFunction function again with the same argument, it first checks whether it has already gotten that value in its cache. If that's the case, the caches value will be returned, which saves on execution time. Else, if it's not cached, it will calculate the value and store it afterwards.
We call the addFunction function three times with the same value: on the first invocation, the value of the function when num is equal to 10 isn't cached yet. The condition of the if-statement num in cache returns false, and the else block gets executed: Calculated! 20 gets logged, and the value of the result gets added to the cache object. cache now looks like { 10: 20 }.
The second time, the cache object contains the value that gets returned for 10. The condition of the if-statement num in cache returns true, and 'From cache! 20' gets logged.
The third time, we pass 5 * 2 to the function which gets evaluated to 10. The cache object contains the value that gets returned for 10. The condition of the if-statement num in cache returns true, and 'From cache! 20' gets logged.

Ref: https://bit.ly/3jFRBje