1。如果你需要一个临时的唯一ID,请生成随机字符串。 这个例子将为你生成一个随机字符串:constrandomStringMath。random()。toString(36)。slice(2);console。log(randomString);outputr0zf1xfqcr(thestringwillberandom) 2。从电子邮件中提取域名, 你可以使用substring()方法来提取电子邮件的域名。letemailxyzgmail。legetDomainemail。substring(email。indexOf()1);console。log(getDomain);outputgmail。com 3。用这个例子检测暗模式,你可以检查用户是否在使用暗模式(然后你可以根据暗模式更新一些功能)constisDarkModewindow。matchMediawindow。matchMedia((preferscolorscheme:dark))。 4。检查元素是否被聚焦 在JavaScript中检测元素是否具有焦点,可以使用Document对象的只读属性activeElement。constelemdocument。querySelector(。textinput);constisFocuselemdocument。activeEisFocuswillbetrueifelemwillhavefocus,andisFocuswillbefalseifelemwillnothavefocus 5。检查数组是否为空 此单行程序将让你知道数组是否为空。letarr1〔〕;letarr2〔2,4,6,8,10〕;constarr1IsEmpty!(Array。isArray(arr1)arr1。length0);constarr2IsEmpty!(Array。isArray(arr2)arr2。length0);console。log(arr1);outputtrueconsole。log(arr2);outputfalse 6。重定向 你可以使用JavaScript将用户重定向到任何特定的URL。constredirecturllocation。hrefurlcallredirect(url)wheneveryouwanttoredirecttheusertoaspecificurl 7。检查变量是否为数组 你可以使用Array。isArray()方法检查任何变量是否为数组。letfruits〔apple,banana,mango,orange,grapes〕;constisArray(arr)Array。isArray(arr);console。log(isArray。(fruit));outputfalseconsole。log(isArray。(fruits)),outputtrue