

JavaScript undefined Check in Go
source link: http://siongui.github.io/2017/12/30/js-undefined-test-in-go/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sometimes we need to check if something exists or not in JavaScript. For example, old browsers may not support localStorage API, so we need to check before we use. In this post, we will show you how to do undefined check in Go/GopherJS.
Test if It's Undefined
Use localStorage as example, we will check if it is undefined in the browser.
JavaScript
if (window.localStorage === undefined) { console.log("Your browser does not support localStorage API"); } else { console.log("Your browser supports localStorage API"); }
Open your console and you will know if your browser support it or not.
GopherJS
GopherJS provides js.Undefined Object for undefined test. The above code in Go/GopherJS is as follows:
import ( "github.com/gopherjs/gopherjs/js" ) if js.Global.Get("localStorage") == js.Undefined { println("Your browser does not support localStorage API") } else { println("Your browser supports localStorage API") }
Run Code on GopherJS Playground
The full code example of this post is on my GitHub.
For real-world example, see [3].
References:
[1][Golang] undefined Test in GopherJS
Recommend
-
107
JavaScript — Null vs. UndefinedLearn the differences and similarities between null and undefined in JavaScript
-
107
理解JavaScript中null、undefined和NaN
-
42
使用JavaScript中undefined小技巧
-
40
Welcome to Rant.js, a new series I’m starting based on short, mostly opinions, on anything that interests me in the JavaScript world. These articles, while not entirely factual, may provide an interesting look into the da...
-
50
原文地址:Understanding JavaScript’s ‘undefined’ 原文作者:Angus Croll 译文出自:掘金翻译计划 本文永久链接:github.com/xitu/gold-m… 译者:yanyixin 校对者:noahziheng
-
43
One aspect of JavaScript development that many developers struggle with is dealing with optional values. What are the best strategies to minimize errors caused by values that could be null , undefined
-
18
TypeOfNaNWhy is My Recursive Function Returning Undefined in JavaScript?Nick Scialli • November 13, 2020 • 🚀 1 minute read
-
6
How to Check if a Property is Undefined in JavaScript Nov 24, 2021 To check if an object property key was equal to undefined you can do: if (obj[key] === undefined) {...
-
15
How to Check If an Object Property is Undefined in JavaScript 972 views 1 year ago Javascript
-
10
Javascript Check If an Object Is Null or Undefined While working in javascript, often we encounter a requirement that is to determine if the object is null or not. We might require this check to verify that th...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK