w3resource

JavaScript: Core Objects

JavaScript : Core Objects

List of JavaScript core objects

Name of the object Version
Array Object Implemented in JavaScript 1.1
Boolean Object Implemented in JavaScript 1.1
Date Object Implemented in JavaScript 1.0
Function Object Implemented in JavaScript 1.1
Java Implemented in JavaScript 1.2
JavaArray Implemented in JavaScript 1.1
JavaClass Implemented in JavaScript 1.1
Java Implemented in JavaScript 1.1
JavaPackage Implemented in JavaScript 1.1
Math Object Implemented in JavaScript 1.0
netscape Implemented in JavaScript 1.1
Number Object Implemented in JavaScript 1.1
Object Implemented in JavaScript 1.0
Packages Implemented in JavaScript 1.1
RegExp Object Implemented in JavaScript 1.2
String Object Implemented in JavaScript 1.0
sun Implemented in JavaScript 1.1

Previous: JavaScript Object
Next: Javascript Array Objects - Properties and Methods

Test your Programming skills with w3resource's quiz.



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