• Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms & Conditions
Friday, November 6, 2026
freevirtualsolutions.com
  • Home
  • Virtual Assistant
  • Enterprise Technology
  • Customer Support
  • Marketing Support
  • Workforce Management
No Result
View All Result
  • Home
  • Virtual Assistant
  • Enterprise Technology
  • Customer Support
  • Marketing Support
  • Workforce Management
No Result
View All Result
Free Virtual Solutions
No Result
View All Result
Home Enterprise Technology

All the brand new options in ECMAScript 2023 (ES14)

Ermias S. by Ermias S.
3 years ago
in Enterprise Technology
0
399
SHARES
2.3k
VIEWS
Share on FacebookShare on Twitter


The wheel of time has carved out one other yr, and with that comes a brand new official launch of JavaScript: ECMAScript 2023, aka ECMAScript 14. Enhancements this yr embody additions to arrays and help for shebang in ECMAScript information, in addition to the enlargement of image keys for weak collections. The modifications are largely refinements versus something dramatic. However, the compound impact is to proceed evolving the language. Right here’s a take a look at what’s new in JavaScript in 2023.

Understanding the spec

Earlier than we dig into the main points of the brand new model, it’s value speaking concerning the ECMAScript specification. You may all the time discover the newest model of the ECMAScript spec hosted at https://tc39.es/ecma262/. If you wish to take a look at the specification in a selected yr, you may append that to the URL. For instance, this yr’s spec is hosted at https://tc39.es/ecma262/2023/. 

To seek out what’s new in a current launch, you may scan the intro (for instance, anchored right here). This provides you a breakdown of what shipped with every new launch. You’ll find out particulars a few given characteristic by trying to find it. (Possibly in a future model, we’ll have a hyperlink to every characteristic!) 

The ECMAScript specification is a powerful doc that serves as each the fundamental reference for builders and educators in addition to the official technical specification for JavaScript engine implementors. That is fairly a balancing act, which the spec manages properly. It may be a bit cumbersome as a consumer’s information to the language due to how a lot data it holds.

One other factor to know concerning the spec is that it’s actually a residing doc that grows interactively because the language is used within the wild. Oftentimes, a brand new characteristic is added to the official specification after it has been informally accepted by the consumer group. We will see this with the shebang syntax this yr, for instance. As soon as a characteristic has been codified and standardized by the specification, the spec serves as a brand new steady foundation for additional innovation of that characteristic. 

Typically, the ECMAScript specification introduces groundbreaking concepts. An instance is the adoption of the async/await syntax, which was influenced by C#. 

As a language, JavaScript has developed by leaps and bounds from the times of cut-and-paste rollover results. The ECMAScript specification course of has performed an enormous position in that evolution. 

Now, let’s check out the brand new options launched to JavaScript in 2023.

Array.prototype.toSorted

Let’s start with the brand new array methodology, toSorted().

toSorted has the identical signature as Array.prototype.type(), nevertheless it creates a brand new array as an alternative of working on the array itself. Here is the brand new array methodology in Itemizing 1.

Itemizing 1. type() versus toSorted()


let arr = [5,4,2,3,1]
arr === arr.type(); // true - [1, 2, 3, 4, 5]

arr === arr.toSorted(); // false - [1, 2, 3, 4, 5]

toSorted(), like type(), additionally accepts a single non-obligatory argument, a comparator perform. For instance, we might use toSorted() to create a brand new array in descending order, as in Itemizing 2.

Itemizing 2. Utilizing the comparator perform


const numbers = [10, 5, 2, 7, 3, 9, 1, 6, 4]; 
const sortedNumbers = numbers.toSorted((a, b) => { 
  return b - a; 
}); 
console.log(sortedNumbers); // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Allow us to additionally make notice that toSorted() will be utilized to arrays of objects. In that case, you need to present a comparator perform that makes use of information on the objects, since there’s no pure ordering for objects. You may see an instance in Itemizing 3.

Itemizing 3. toSorted() with objects


// Evaluating objects
const objects = [{ name: "John", age: 30 }, { name: "Jane", age: 25 }, { name: "Bill", age: 40 }, { name: "Mary", age: 20 }];
const sortedObjects = objects.toSorted((a, b) => {
  return a.title.localeCompare(b.title); 
});
console.log(sortedObjects);
//[{"name":"Bill","age":40},{"name":"Jane","age":25},{"name":"John","age":30},{"name":"Mary","age":20}]

Itemizing 3 kinds the objects by the title area.

Array.prototype.toReversed

Like toSorted() and type(), toReversed() is the duplicating cousin of reverse(). Itemizing 4 has a few fast examples of utilizing toReversed(), together with making use of it to things with a comparator perform.

Itemizing 4. toReversed()


["a","b","c","d","e"].toReversed(); // ['e', 'd', 'c', 'b', 'a']

Array.prototype.with

The brand new with() methodology permits you to modify a single factor primarily based on its index, and get again a brand new array. So, if you understand the index and the brand new worth, this methodology makes it very simple. Observe that with() is the copying companion to set(). Itemizing 5 provides a easy instance.

Itemizing 5. Changing a component with with()


const arr4 = ["I", "am", "the", "Walrus"];

// Change the string "Walrus" with "Octopus".
const newArr4 = arr4.with(3, "Ape Man");

console.log(newArr4);

Array.prototype.findLast

The findLast() methodology permits you to get the ultimate occasion of an identical factor from an array. If no matching factor is discovered, it returns undefined. A easy instance is given in Itemizing 6, the place we get the final even quantity from an array. 

Itemizing 6. Discover the final even quantity with findLast()


const arr = [54, 34, 55, 75, 98, 77];

const lastEvenIndex = arr.findLast((factor) => {
  return factor % 2 === 0;
});

console.log(lastEvenIndex); // 98

findLast() additionally helps passing in a “thisArg” to set the context. That’s to say, the second argument will inform the primary argument perform what the this key phrase will consult with. You may see this in motion in Itemizing 7, the place we use a customized object to seek out the primary factor evenly divisible by 5.

Itemizing 7. Utilizing the thisArg


const arr6 = [54, 34, 55, 75, 98, 77];
const myObject = {testCase: 5};
const lastEvenIndex = arr5.findLast((factor) => {
  return factor % myObject.testCase === 0;
}, myObject);

console.log(lastEvenIndex); // 75

Array.prototype.findLastIndex

findLastIndex() works precisely like findLast(), besides it provides you the index of the factor matching as an alternative of the factor itself. For instance, Itemizing 8 reveals easy methods to discover the index of the final factor evenly divisible by 6.

Itemizing 8. Discover the index of factor with findLastIndex()


const arr = [54, 34, 55, 75, 98, 77];
arr.findLastIndex(x => x % 6 === 0); // 0

Array.prototype.toSpliced

To this point, all of the strategies we’ve described additionally apply to TypedArray. The ultimate new array methodology, toSpliced(), solely exists on Array. The toSpliced() methodology is the copying model of splice()—that acquainted outdated swiss military knife of JavaScript array manipulation. 

Let’s say we have now an array of  colours and we have to insert a few new ones (pink and cyan) within the center. You may see this in Itemizing 9. Keep in mind, this creates a brand new array relatively than modifying the unique.

Itemizing 9. toSpliced() in motion


const arr = ["red", "orange", "yellow", "green", "blue", "purple"]; const newArr = arr.toSpliced(2, 1, "pink", "cyan"); console.log(newArr); 
// ["red", "orange", "pink", "cyan", "green", "blue", "purple"]
console.log(newArr[3]);
// 'cyan'
console.log(arr[3]);
// ‘inexperienced’

Official shebang help

A shebang is old-school Unix discuss for a hashtag adopted by an exclamation level: #! (the place “bang” is slang for “!”). Since time immemorial, a remark on the head of a file that begins with #! tells the shell that what you’ve received right here is an executable script, and what engine to make use of to run it. For instance, Itemizing 10 is a typical bash script.

Itemizing 10. Shebang on bash script: howdy.sh


#!/bin/bash

echo "Whats up, world!"

You may run a file just like the one in Itemizing 10 immediately with ./howdy.sh.

You are able to do related with JavaScript, as proven in Itemizing 11.

Itemizing 11. Shebang in JS: howdy.js


#!/usr/bin/env node

console.log("Whats up, world!");

Itemizing 11 tells the working system to make use of the node program to run this script. Now, you may simply kind ./howdy.js to run it. With out the #!, that would not work.

Shebang help is a kind of characteristic updates within the spec that has already been carried out and unofficially adopted in a number of contexts. 

Symbols as keys on weak collections

The ultimate new characteristic in ECMAScript 14 is the enlargement of what can be utilized as keys in weak collections.

Weak collections are a bit esoteric in comparison with on a regular basis JavaScript utilization. In programming, a weak reference is one which shall be disposed of if it might in any other case be rubbish collected. Put one other manner, a weak reference by itself will not be sufficient to maintain the reference goal from being disposed of by the rubbish assortment algorithm (that is why it is a weak reference).

You may be taught extra about weak references and when they’re helpful right here. There’s additionally a very good dialogue right here.

What ES14 does is permit using most Images as keys in a group, whereas earlier than you can solely use an object. If you happen to marvel what a Image is, you aren’t alone. Right here is the place you may be taught extra.

The brand new characteristic primarily makes it simpler to make use of weak references in collections like WeakMaps by easing the restrictions on what can be utilized for keys. A easy instance is proven in Itemizing 12.

Itemizing 12. Utilizing a logo as a key on WeakMap


var map = new WeakMap(); // create a weak map
perform useSymbol(image)

let mySymbol = Image(“FooBar”);
useSymbol(mySymbol);
useSymbol(mySymbol);
useSymbol(mySymbol);

delete mySymbol; // No stay references are left to mySymbol, so we will rely on the rubbish collector eliminating the entry within the weakMap when it runs (finally)

Itemizing 12 is modified from the above linked StackOverflow reply. On this instance, the aim is to permit for calling the counter from an exterior caller and to eliminate the map entry when there aren’t any references left. The code itself has no manner of understanding when the reference is not wanted, and in case you use a traditional Map, you’ll get a reminiscence leak. It’s because the code will maintain onto the reference even after the shopper calling it not wants it. On this case, the place we use the WeakMap, we will rely on the rubbish assortment dropping the map entry when no extra references exist to the important thing image.

Conclusion

Though 2023 was a comparatively quiet yr for JavaScript, ECMAScript 14 added some helpful options and stored the official spec up to the mark with the true world. Coming within the subsequent model, we’ll have a slew of modifications together with a shiny new Temporal API for dealing with datetimes.

Copyright © 2023 IDG Communications, Inc.



Source_link

Tags: ECMAScriptES14features
Previous Post

Hope and fact | Seth’s Weblog

Next Post

Monitoring Worker Hours with an Excel Time Spreadsheet

Next Post
Monitoring Worker Hours with an Excel Time Spreadsheet

Monitoring Worker Hours with an Excel Time Spreadsheet

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Customer Support (1,886)
  • Enterprise Technology (4,742)
  • Marketing Support (4,029)
  • Virtual Assistant (4,349)
  • Workforce Management (1,557)

Free Virtual Solutions

Welcome to freevirtualsolutionsThe goal of freevirtualsolutions is to give you the absolute best news sources for any topic! Our topics are carefully curated and constantly updated as we know the web moves fast so we try to as well.

Category

  • Customer Support
  • Enterprise Technology
  • Marketing Support
  • Virtual Assistant
  • Workforce Management

Recent Post

  • How To Report SEO Performance When Analytics Data Is Incomplete
  • Don’t Let the AI Race Distract You From Your Team’s Performance
  • Forensic Accounting for Business Interruption Claims
  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms & Conditions

Copyright © 2023 Freevirtualsolutions.com | All Rights Reserved.

No Result
View All Result
  • Home
  • Virtual Assistant
  • Enterprise Technology
  • Customer Support
  • Marketing Support
  • Workforce Management

Copyright © 2023 Freevirtualsolutions.com | All Rights Reserved.

Chat with us

Hi there! How can I help you?