위코드 사전스터디 과정 중 JavaScript의 String(문자열)에 대해서 정리해본다
let greeting = 'Hello world'
let myName = 'Code Kim'
console.log(greeting); // Hello world 출력
console.log(myName); // Code Kim 출력
console.log('Hello world'+' '+'Code Kim'); // Hello world Code Kim
console.log(greeting+' '+myName); // Hello world Code Kim
console.log(2+2); // 4 (typeof Number)
console.log(22); // 22 (typeof Number)
console.log('2'+'2'); // 22 (typeof String)
console.log(2+"2"); // 22 (typeof String) > 숫자와 문자를 합치면 문자
🎶 더하기 지식!
문자열 연결 연산자(+연산자)는 피연산자 중 하나 이상이 문자열인 경우 문자열 연결 연산자로 동작한다. 그 외의 경우는 산술 연산자로 동작한다.
'1'+2; // '12'(문자)
1+'2'; // '12'(문자)
1+2; // 3(숫자)
const myString = 'Hello! wecode!!';
console.log(myString); // Hello! wecode!!
변수 myString의 길이를 구하고 싶을 때
console.log(myString.length); // 15
변수가 아니어도 된다
console.log('Hello! wecode!!'.length); // 15
console.log(
'Hello! wecode!! is ',
'Hello! wecode!! is '.length);// Hello! wecode!! is 15
console.log('Hello! wecode!! is '+'Hello! wecode!! is '.length); //
Hello! wecode!! is15