w3resource

JavaScript - Exercises, Practice, Solution

What is JavaScript?

JavaScript is a cross-platform, object-oriented scripting language. It is a small and lightweight language. Inside a host environment ( a web browser), JavaScript can be connected to the objects of its environment to provide programmatic control over them.

JavaScript contains a standard library of objects, such as Array, Date, and Math, and a core set of language elements such as operators, control structures, and statements. Core JavaScript can be extended for a variety of purposes by supplementing it with additional objects.

The best way we learn anything is by practice and exercise questions. We have started this section for those (beginner to intermediate) who are familiar with JavaScript. Hope, these exercises help you to improve your JavaScript coding skills. Currently following sections are available, we are working hard to add more exercises. Happy Coding!

List of JavaScript Exercises :

More to Come !

Popularity of Programming Language
Worldwide, Oct 2022 compared to a year ago:

`
Rank Change Language Share Trend
1 Python 28.3 % -1.8 %
2 Java 17.2 % -0.9 %
3 Javascript 9.69 % +0.4 %
4 C# 7.2 % -0.2%
5 C/C++ 6.45 % -0.4 %
6 PHP 5.39 % -0.9 %
7 R 4.03 % +0.3 %
8 up arrow TypeScript 2.17 % +1.1 %
9 down arrow Objective-C 2.16 % +0.2 %
10 up arrow Go 2.08% +0.5 %
11 down arrow Swift2.05% +0.4 %
12 down arrow Kotlin 1.81 % +0.1 %
13 up arrow Rust 1.57 % +0.8 %
14 down arrow Matlab 1.52 % +0.1 %
15 Ruby 1.13 % +0.1%
16 down arrow VBA 0.96 % -0.3 %
17 up arrow Scala 0.78 % +0.3 %
18 up arrow Ada 0.76 % +0.2 %
19 down arrow Dart 0.75 % +0.1 %
20 down arrow Visual Basic 0.64 % -0.2 %
21 up arrow Lua 0.61 % +0.2 %
22 down arrow Abap 0.48 % -0.1 %
23 down arrow Groovy 0.44 % -0.1 %
24 up arrow Cobol 0.32 % -0.0 %
25 Julia 0.28 % -0.1 %
26 down arrow Perl 0.28 % -0.1 %
27 Haskell 0.28 % +0.0 %
28 Delphi/Pascal 0.14 % +0.1 %

Source : https://pypl.github.io/PYPL.html

TIOBE Index for September 2022

Sep 2022 Sep 2021 Change Programming Language Ratings Change
1 2 up arrow Python 15.74% +4.07%
2 1 down arrow C 13.96% +2.13%
3 3 Java 11.72% +0.60%
4 4 C++ 9.76% +2.63%
5 5 C# 4.88% -0.89%
6 6 Visual Basic 4.39% -0.22%
7 7 JavaScript 2.82% +0.27%
8 8 Assembly language 2.49% +0.07%
9 10 up arrow SQL 2.01% +0.21%
10 9 down arrow PHP 1.68% -0.17%
11 24 up arrow Objective-C 1.49% +0.86%
12 14 up arrow Go 1.16% +0.03%
13 20 up arrow Delphi/Object Pascal 1.09% +0.32%
14 16 up arrow MATLAB 1.06% +0.04
15 17 up arrow Fortan 1.03% +0.02%
16 15 down arrow Swift 0.98% -0.09%
17 11 down arrow Classic Visual Basic 0.98% -0.55%
18 18   R 0.95% -0.02%
19 19 Perl 0.72% -0.06%
20 13 down arrow Ruby 0.66% -0.62%

Source : https://www.tiobe.com/tiobe-index/

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Note : Since JavaScript is a loosely-typed, dynamic and expressive language, you may accomplish the same task in various ways. Therefore the ways (solution of the exercises) described here are not the only ways to do stuff. Rather, it would be great, if this helps you anyway to choose your own methods.

[ Want to contribute to JavaScript exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]



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