å
JavaScriptã§ãµãã¼ãããã¦ãããã¼ã¿åã¯ä»¥ä¸ã®ç©ãããï¼
- æ°å¤ (number)
- æåå (string)
- çå½å¤ (boolean)
- é¢æ° (function)
- ãªãã¸ã§ã¯ã (object)
- æªå®ç¾© (undefined)
ããå¤ã®åã¯typeofãç¨ãããã¨ã§èª¿ã¹ããã¨ãã§ããï¼
js> typeof 42 number js> typeof "JavaScript" string js> typeof true boolean js> typeof print function js> typeof new Date() object js> typeof undefined undefined js>
æ°å¤
æ´æ°
JavaScriptãé常ã®ããã°ã©ãã³ã°è¨èªã¨åæ§ã«æ°ã表ç¾ã§ãï¼å æ¸ä¹é¤ã®ç°¡åãªè¨ç®ãè¡ããã¨ãã§ããï¼
js> 42 42 js> 3 + 2 5 js> 2 - 5 -3 js> 4 * 3 12 js> 6 / 3 2 js> 7 % 3 1 js> - 42 -42
ãã ãï¼'/'ã¯æ´æ°ã®åãæ±ããã®ã§ã¯ãªãï¼æµ®åå°æ°ç¹æ°ãè¿ãï¼
js> 2 / 3 0.6666666666666666
16é²è¡¨è¨
æ°å¤ã®ãªãã©ã«ã¯10é²ä»¥å¤ã®è¡¨ç¾ãã§ãï¼8é²æ°ã¨16é²æ°ã§è¡¨ç¾ã§ããï¼ãã ãï¼8é²æ°ã®ãªãã©ã«ã¯ECMA-262 revision3ã§é¤å»ããï¼ä¸ä½äºææ§ã®ããã ãã«åå¨ããï¼
js> 010 8 js> 0x10 16
æµ®åå°æ°ç¹æ°
æµ®åå°æ°ç¹æ°ã¯ä»¥ä¸ã®ããã«è¡¨ç¾ã§ãã
js> 3.14159 3.14159 js> .01 0.01 js> 3.00E8 300000000 js> 6.63e-34 6.63e-34
Infinityã¨NaN
ç¹æ®ãªå¤ã¨ãã¦Infinityã¨NaNãããï¼ä¾ãã°ï¼æ°å¤ã0ã§å²ãã¨ç¡é大ã表ãInfinityã«ãªã
js> 1 / 0 Infinity js> -1 / 0 -Infinity
0ã0ã§å²ãNaN(Not a Number)ã¨ããå¤ã«ãªãï¼ãããï¼NaNã®åã¯numberã§ããï¼
js> 0 / 0 NaN js> typeof NaN number js>
InfinityãNaNã§ãããã©ããã¯isFiniteãisNaNã§èª¿ã¹ããã¨ãã§ããï¼
js> isFinite(1/0) false js> isNaN(0/0) true
æ°å¦é¢æ°
æ°å¦é¢æ°ã¯Mathãªãã¸ã§ã¯ããç¨ãã¦è¨ç®ãããã¨ãã§ããï¼Mathãªãã¸ã§ã¯ãã«ã¯ä»¥ä¸ã®ãããªå®æ°ãé¢æ°ãããï¼
js> Math.PI 3.141592653589793 js> Math.E 2.718281828459045 js> Math.sin(Math.PI/2) 1 js> Math.log(Math.E) 1 js> Math.abs(-1) 1 js> Math.ceil(1.2) 2 js> Math.min(3, 1) 1 js> Math.pow(2, 10) 1024 js> Math.sqrt(65536) 256 js> Math.random() 0.4507465665911181 js>
æåå
æååã¯""ã§å²ãã''ã§å²ãï¼""ã¨''ã®éãã¯'ã¨"ã®ã©ã¡ããã¨ã¹ã±ã¼ãããã«è¨è¿°ã§ãããã§ããï¼ãã®ä»ã®ã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ã¯ã©ã¡ãã§ãè¨è¿°ãããã¨ãã§ããï¼
js> "hoge" hoge js> 'hoge' hoge js> "first line\nsecond line" first line second line js> 'first line\nsecond line' first line second line
'+'ãç¨ãããã¨ã§æååã®é£çµãè¡ããã¨ãã§ããï¼
js> "Java" + "Script" JavaScript
ã¾ãï¼lengthã¡ã½ãããç¨ãã¦æååã®é·ããè¨ç®ãããã¨ãã§ãã
js> "Mozilla".length 7
æååé¢æ°
以ä¸ã®ãããªæååé¢æ°ãããï¼
js> "JavaScript".charAt(4) S js> "JavaScript".indexOf("Script") 4 js> "Java".concat("Script") JavaScript js> "This sentence will pe splitted".split(" ") This,sentence,will,pe,splitted js> "JavaScript".substr(4, 6) Script js> "JavaScript".search(/Script/) 4 js> "JavaScript".toLowerCase() javascript js> "JavaScript".toUpperCase() JAVASCRIPT
Unicode
JavaScriptã¯Unicodeã«å¯¾å¿ãã¦ããæ¥æ¬èªã«å¯¾ãã¦ãæååé¢æ°ãæ£ããç¨ãããã¨ãã§ããï¼ã¯ããªãã ãï¼ãã¡ã®ç°å¢ã§ã¯ãã¾ããããªãï¼
js> "ããã°ãããã·ã¨".length 24 // æ¬å½ã¯8 js> "ããã°ãããã·ã¨".charAt(2) ? // æ¬å½ã¯'ã°'
ãã£ã¡ã®Shellãªã©ã§å®è¡ããã¨ãã¾ãããï¼
"ããã°ãããã·ã¨".length 8 "ããã°ãããã·ã¨".charAt(2) ã°
åå¤æ
æ°å¤ã¨æååã«å¯¾ãã¦+æ¼ç®åãç¨ããã¨æååã«åå¤æããé£çµãããï¼
js> 42 + " is the number" 42 is the number js> "The number is " + 42 The number is 42
ãã®ä»ã®æ¼ç®åã ã¨æååãæ°å¤ã«æååã«åå¤æãããï¼
"37" * "5" 185
å¤æ°
å¤æ°ã®å®£è¨ã¯varãç¨ãã¦è¡ãï¼
js> var a = 42
宣è¨ãã¦ããªãèå¥åãè©ä¾¡ããã¨ReferenceErrorãèµ·ããï¼
js> b typein:5: ReferenceError: b is not defined
宣è¨ãã¦ãããä»£å ¥ãã¦ããªãå¤æ°ã¯å¤undefinedãæã¤ï¼
js> var c js> c js> print(c) undefined js>
å¤æ°ã®ã¹ã³ã¼ã
JavaScriptã®ã¹ã³ã¼ãã¯ã°ãã¼ãã«ãªã¹ã³ã¼ãã¨é¢æ°ã®ã¹ã³ã¼ããæã£ã¦ããï¼
var g = 42; function foo(a) { var l = 28; return l + a; } print(g); print(l);
ä¸ã®ã¹ã¯ãªãããå®è¡ããã¨ï¼é¢æ°ã®ä¸ã§å®£è¨ããlã«ã¯ã¢ã¯ã»ã¹ãããã¨ãã§ããã«ReferenceErrorãçºçããï¼
$ js -f sample.js 42 sample.js:9: ReferenceError: l is not defined
ãããã¯ã¹ã³ã¼ãã¯åå¨ãã(JavaScript1.7ã§ã¯è¿½å ããã¦ããããã )ï¼ãã®ããï¼ãããã¯å ã§å®£è¨ãããé¢æ°ã¯ãããã¯å¤ã§ãå©ç¨ãããã¨ãã§ããï¼
js> var sum = 0; js> for (var i = 0; i < 10; i++) { sum += i; } 45 js> sum 45 js> i 10 js>
varã使ããªãå¤æ°å®£è¨
varãç¨ããªãã¦ãå¤æ°ã«ä»£å ¥ãããã¨ã§ãã®å¤æ°ãå©ç¨ã§ããããã«ãªãï¼ãããï¼ãã®å¤æ°ã¯å¸¸ã«ã°ãã¼ãã«ãªã¹ã³ã¼ããæã¡ï¼ãã¾ãæ¨å¥¨ããã¦ããªãï¼ã¯ããªãã ãï¼JavaScript Shellã§ã¯varã使ããªãã¦ãé¢æ°ãã¼ã«ã«ãªå¤æ°ã«ãªã£ã¦ãã
js> function foo(a) { var l = 42; return l + a; } js> l typein:12: ReferenceError: l is not defined js> function bar(a) { g = 42; return g + a; } js> g typein:5: ReferenceError: g is not defined // æ¬å½ã¯ã¢ã¯ã»ã¹ã§ããã¯ã js>