Cloaking in SEO refers to a “black hat” tactic that is generally frowned upon. When a website is cloaking, they are serving the human user a different set of content to the search engine crawlers and bots as a way to try artificially rank their page for the topic.

They recognise the crawler usually through their User-Agent, IP address or HTTP headers. This can also be done in JavaScript, as search engines would typically not execute the JavaScript on the page, so the raw HTML page compared to JS may be incredibly different, however, it is becoming more common for search engines to execute the JS as well these days.

If search engines like Google catch a website doing this, they risk being penalised in search, which is very hard to come back from.