How to Check HTTP Referrer with JavaScript or jQuery?

Using JS I need to detect if a user has come to my website from a social media platform like Facebook, Twitter, or Instagram. I know in PHP HTTP referrer can be used to check this but how to achieve this in JavaScript. Can someone please provide a simple example of how to check the HTTP referrer using JavaScript?

In JavaScript, the document object provides a property called referrer . This property holds the URL of the page that linked to the current page. You can use this property to check where your visitor came from.

var referrer = document.referrer;

if (referrer.includes("facebook.com")) {
    console.log("User came from Facebook");
} 
else if (referrer.includes("twitter.com")) {
    console.log("User came from Twitter");
} 
else if (referrer.includes("instagram.com")) {
    console.log("User came from Instagram");
} 
else if (referrer == "") {
    console.log("Direct user");
}
else {
    console.log("User came from other source");
}

If the referrer property is empty, it means the user visited the site directly. You can adjust the code to include more social media platforms or customize the behavior based on your specific requirements.