[js]データ型(datatype、let vs var;hosting-es 6)
3945 ワード
let vs var
変数を宣言するときにletを無条件に使用する
varは使用されません
変数の宣言には2つのタイプがあります
Mutable-Let:rw(読み取り/書き込み)メモリの値は、読み取り/書き込みで変更できます.
可変-cont:r(読み取り専用)の値を指定すると、読み取り可能にロックされます.より望ましい方法.
Immutable data types: premitive types, fronze objects (i.e. object.freeze())
Mutable data types: all objects by default are mutable in JS
favor immutable data type always for a few reaseons;
-security
-thread safety
-reduce human mistakes
variable datatypes
number
const count = 17; // integer
const size = 17.1; // decimal number
console.log('value: ${count}, type: ${typeof count}'); // value:17, type:number
console.log('value: ${size}, type: ${typeof size}'); // size:17.1, size:number
//number-speicla numeric values:
const infinity = 1 / 0; //무한의 값을 infinity라고 함.
const negativeInfinity = -1 / 0; //-infinity
const nAn = 'not a number' /2; //숫자가 아닌 경우
다양한 요소를 가져올 때 0인지 아닌지 숫자인지 아닌지 확인하지 않고 가져오면 오류가 남.
연산할 때 그 값이 정말 valed한 값인지를 확인해야함.
変数を宣言します。
//string
const char = 'c';
const brendan = 'brendan';
const greeting = 'hello' + brendan;
console.log('value: ${greeting}, type: ${typeof greeting}');
// -> value: hello brendan, type: string
const helloBob = 'hi ${brendan}!'; //template literals (string)
console.log('value: ${helloBob}, type: ${typeof helloBob}');
// -> value: hi brendan!, type: string
template literals은 ''을 이용해 string을 더욱 효율적으로 사용함.
기존의 방식: console.log('value: ' + helloBob + ' type: ' + typeof helloBob);
//boolean (참과 거짓)
//false: 0, null, undefined, NaN(not a number), ''
//true: any other value
const canRead = true;
const test = 3 < 1; // false
console.log('value ${canRead}. type: ${typeof canRead}');
// -> value: true, type: boolean
console.log('value ${test}. type: ${typeof test}');
// -> value: false, type: boolean
//null(내가 명확하게 너는 텅텅 비어있는 값이야 하고 지정해주는 것)
let nothing = null;
console.log('value: ${nothing}, type: ${typeof noting}');
// -> value: null, type: object
//undefined(선언은 되었지만 아무것도 값이 지정되어있지 않음)
let x; OR let x = undefined;
console.log('value: ${x}, type: ${trpeof x}');
// -> value: undefined, type: undefined
//symbol, create unique identifiers for objects(정말 고유한 식별자가 필요할 때 사용,
다른 모듈이나 다른 곳에서 같은 string을 썼을 때 symbol은 동일한 식별자로 인식하지 않고
다른 것으로 간주함.)
const symbol1 = Symbol('id')
const symbol2 = Symbol('id')
console.log(symbol1 === symbol2); //false
string이 똑같다면 동일한 symbol을 만들고 싶다면 Symbol.for('id')사용.
const gSymbol1 = Symbol.for('id');
const gSymbol2 = Symbol.for('id');
console.log(gSymbol1 === gSymbol2); //true
symbol은 바로 출력하면 에러가 뜸.따라서.description 붙여서 string으로 변환해서 출력해야함.
console.log('value: ${symbol1.description}, type: ${trpeof x}');
// -> value: id, type: symbol
//Dynamic typing: dynamically typed language
//(js은 동적(움직이는 성격의 것) c,java는 정적임)
let text = 'hello';
console.log('value: ${tet}, type: ${typeof text}');
//-> value: hello, type: string
text = 1;
console.log('value: ${text}, type: ${typeof text}');
//-> value: 1, type: number
text = '7' + 5;
console.log('value: ${text}, type: ${typeof text}');
//-> value: 75, type: string
text = '8' / '2'; 뭐야 string인데 나누기를 사용했잖아? 그렇다면 number로 변경해야지!
console.log('value: ${text}, type: ${typeof text}');
//-> value: 4, type: number
Reference
この問題について([js]データ型(datatype、let vs var;hosting-es 6)), 我々は、より多くの情報をここで見つけました https://velog.io/@minimal_time/js-데이터-타입-datatype-let-vs-var-hosting-es6テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol