A Comprehensive Overview of Server-Side Global Functions in Salesforce B2C Commerce Cloud

In development, it’s essential to have access to useful functions that can make your work more efficient. Salesforce B2C Commerce Cloud offers several global functions which are helpful when working on the server side. These functions have a range of capabilities, from encoding and decoding characters in a URI to executing JavaScript code from a string. This article will explore these functions in detail and discuss how developers can use them to streamline their development process.

encodeURI and encodeURIComponent

				
					encodeURI('https://mozilla.org/?x=шеллы');
// https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B

encodeURIComponent('шеллы');
//?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B

				
			

These functions are used to escape characters in a URI or URI component. The encodeURI function escapes characters in a URI, while the encodeURIComponent function escapes characters in a URI component. Both functions take a string that contains a URI or URI component and return a copy of the input string with certain characters replaced by hexadecimal escape sequences.

decodeURI and decodeURIComponent

				
					decodeURI('https://www.google.com/search?q=decodeuri%20example%20%C3%A5%D0%B5%D0%BB');
// https://www.google.com/search?q=decodeuri example åел

decodeURIComponent('query=?/learning tō dėcōdė');
//query=?/learning tō dėcōdė

				
			

These functions are used to unescape characters in a URI component. The decodeURI function unescapes characters in a URI, while the decodeURIComponent function unescapes characters in a URI component. Both these global functions take a string that contains an encoded URI or URI component and return a copy of the input string with any hexadecimal escape sequences replaced with the characters they represent.

empty

				
					empty('')
// true

empty([]);
// true

empty('a string');
// false

				
			

The empty function is used to test whether a given object is empty. The interpretation of empty is the following: null is always empty:

  • undefined is always empty
  • a string with zero length is empty
  • an array with no elements is empty
  • a collection with no elements is empty.
  • an object returns true if the object is interpreted as empty.

escape and unescape

A drawing of a jail cell with a man in orange.
				
					escape("äöü");
// "%E4%F6%FC"

unescape("%E4%F6%FC"); 
// "äöü"

				
			

The escape function encodes a string by replacing characters with hexadecimal escape sequences. The unescape function decodes an escaped string by replacing hexadecimal character sequences with Unicode characters. Both global functions take a string as a parameter and return a copy of the input string with characters encoded or decoded.

eval

				
					eval('2 + 2');
// 4
				
			

The eval function is used to execute JavaScript code from a string. However, it is deprecated because it can be a potential security risk for server-side code injection. The function takes a string that contains the JavaScript expression to be evaluated or the statements to be executed and returns the value of the executed call or null.

isFinite and isNaN

the infinity of space
				
					isFinite(1000 / 0);
// false

isFinite(100 / 1);
// true

isNaN('100F');
// true

isNaN('0.0314E+2');
// false
				
			

The isFinite function is used to determine whether a specified number is finite. It takes a number as a parameter and returns true if the specified number is finite, false otherwise. The isNaN function tests the specified value to determine if it is not a number. It takes an object as a parameter and returns true if the object is not a number.

parseFloat and parseInt

				
					parseFloat('28.695307297889173');
// 28.695307297889173

parseInt('28.695307297889173');
// 28
				
			

The parseFloat function is used to parse a string into a floating-point number. It takes a string as a parameter and returns the float as a number. The parseInt function uses the specified radix to parse a string into an integer number. If no radix is specified, the function automatically determines the radix based on the input string. These functions return the integer as a number.

Other global functions

Not all global functions that are available are described in this article, but the most important (and used) ones are.

Interested in what these functions are? Then have a look at the documentation!

A room with many papers on the wall with flow charts and documentation

Table of Contents

Facebook
Twitter
LinkedIn