JS - 03_String

송철진·2022년 9월 12일
0

01. getFullName

두 문자열은 + 연산자로 합칠 수 있다

	let writer = "팀 켈러,";
	let bookTitle = "<팀 켈러, 결혼을 말하다>";

	let name = writer + bookTitle;
    console.log(name);
	console.log(writer + " " + bookTitle);
    console.log("팀 켈러," + " " + "<팀 켈러, 결혼을 말하다>");

	console.log(2+2)		// 4
	console.log('2'+'2')	// "22"
    console.log(22)			// 22
	console.log(2+'2')    	// "22"
    console.log(2,'2')		// 2 "2"
    console.log('2','2')	// "2" "2"
    console.log(2,2)		// 2 2
	console.log("111"/2)	// 55.5
    console.log("111"%2)	// 1
    console.log("111"*2)	// 222
    console.log("www"/2)	// NaN
    console.log("www"%2)	// NaN
    console.log("www"*2)	// NaN

string으로 받은 숫자에 사칙연산을 하면 결과값은 숫자이다

02.getLengthOfWord

선언한 변수명에 '.length'를 붙여서 할당한 value의 길이를 알 수 있다

  • value가 string이 아닌 숫자였을 때는 안됨
  • string + 숫자 = string 이다
	let fullName1 = 123141;			 	// 123141
	let fullName2 = 12314+"123445";	 	// "12314123445"
	let fullName3 = "124"+"a한글123";	// "124a한글123"	

	console.log(fullName1.length)	// undefined
	console.log(fullName2.length)	// 11
	console.log(fullName3.length)	// 9
	console.log("The length of SEPTEMBER is ", "SEPTEMBER".length);	
    // "The length of SEPTEMBER is " 5
    console.log("The length of SEPTEMBER is "+ "SEPTEMBER".length); 
    // "The length of SEPTEMBER is 5"

03.averageLength

각 변수에 할당된 String 글자수의 평균 구할 수 있다

	let book1 = "마음아 넌 누구니";
    let book2 = "이동통신과 마이크로파 통신의 실무 이론";
    let length1 = book1.length;
    let length2 = book2.length;
    
    console.log(length1)				// 9
    console.log(length2)				// 21
    console.log((length1+length2)/2)	// 15
profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글