First of all:
This is NOT secure, and i would NOT recommend this for anything security related.
But if you have to do it:
You can use a javascript obfuscator, which basically makes your code unreadable while it still works as i should.
Remember unreadable is not equal to undecodable.
It turns your javascript:
var url = "http://www.example.com";
Into:
var _0x5386=["\x68\x74\x74\x70\x3A\x2F\x2F\x77\x77\x77\x2E\x65\x78\x61\x6D\x70\x6C\x65\x2E\x63\x6F\x6D"];var url=_0x5386[0]
Which is 100% the same code, just unreadable:
var _0x5386=["\x68\x74\x74\x70\x3A\x2F\x2F\x77\x77\x77\x2E\x65\x78\x61\x6D\x70\x6C\x65\x2E\x63\x6F\x6D"];var url=_0x5386[0]
console.log(url); // http://www.example.com
Else take a look at Nick Bull's answer if you want to encode the url, but this will still leave your url readable for the naked eye if reading the javascript code.
This method does also have a weakness to the console window, since anyone could just type in the variable "url" and receive the url.
So all in all, this method will not leave your url in plaintext, but it does have the same console flaw like Nick Bull's answer.
But again, this is NOT secure.