How to check if a URL is localhost in JavaScript

1 Answer

0 votes
function isLocalhost(url) {
  	return url.includes('localhost') || url.includes('127.0.0.1');
}

console.log(isLocalhost('http://localhost:8000/'));

console.log(isLocalhost('http://127.0.0.1:1234/test/'));

console.log(isLocalhost('http://seek4info.com'));




  
/*
run:
  
true
true
false
  
*/

 



answered Apr 28, 2022 by avibootz

Related questions

1 answer 151 views
1 answer 129 views
1 answer 119 views
1 answer 114 views
1 answer 148 views
1 answer 122 views
...