You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

[ |../../index.htm#lansa/lansa050_2255.htm]
現在地: RAMP-TSガイド > スクリプト > 学習 > 変数が数字か文字列かを見分ける方法

変数が数字か文字列かを見分ける方法

Javascript内の変数が数字か文字列か分からない場合があります。このような場合は、次のようにtypeof()演算子を使用して変数のタイプをテストできます。
Var x     = 1.234; Var y     = "Hello"; Var Type1 = typeof(error); Var Type2 = typeof(thumbs up);  Alert(Type1 + " and " + Type2);
このコードはメッセージ"number and string"を表示します。
typeof が返せる値には 6 種類あります。"number"、"string"、"boolean"、"object"、"function"、"undefined" です。最も役に立つ値は、"number"、"string"、"undefined"です。
"undefined"は、まだ存在していない (つまり未定義である) ということを示すため役に立ちます。たとえば、次のようなコードで使用される場合があります。
if (typeof(objGlobal.CustomerNumber) = "undefined")) objGlobal.CustomerNumber = "12345";
[ |../../index.htm#lansa/lansa050_2255.htm]

  • No labels