Preserve plus sign in JavaScript URLSearchParams

Preserve plus sign in JavaScript URLSearchParams

encodeURIComponent() should be used to pass the URL Parameters to preserve the plus(+) sign when using JavaScript URLSearchParams.

Sample JavaScript Code:

let stra = encodeURIComponent( 
	'hello+world'
);
let strb = encodeURIComponent( 
	'test1+test2' 
);
let params = new URLSearchParams( 
	'a=' + 
	stra + 
	'&b=' + 
	strb 
);
console.log(
	params.get( 
		'a' 
	)
);
console.log(
	params.get( 
		'b' 
	)
);

Leave a Reply