2.4 Variables

히진로그·2022년 1월 24일
0

Modern-Javascript

목록 보기
1/14

출처: https://javascript.info/

  • 혼자 읽고 타이핑하며 이해하면서 공부한 흔적 남기기

Variables are used to store this information.

A variable

A variable is a 'named storage' for data.

변수는 데이터의 '이름이 지정된 저장소'이다.

let message;

→ 변수 선언

Now, we can put some data into it by using the assignment operator =

할당 오퍼레이터 '='를 사용해서 변수에 데이터를 넣는다.

let message;

message = 'Hello';

To be concise, we can combine the variable declaration and assignment into a single line

let message = 'Hello';

We can put any value in the box.

We can also change it as many times as we want.

let message;

message = 'Hello!';

message = 'World';

When the value is changed, the old data is removed from the variable.

We can also declare two variables and copy data from one into the other.

2개의 변수를 선언하고 하나의 데이터를 복사할 수 있다.

let hello = 'Hello world';

let message;

message = hello;

alert(hello); // Hello world
alert(message); // Hello world

Variable naming

There are two limitations on variable names in JavaScript:

  1. The name must contain only letters, digits, or the symbols $ and _.

오직 문자, 숫자, $와_의 심볼만 가능

  1. The first character must not be a digit.

첫 번째로 숫자가 올 수 없음.

Constants

To declare a constant(unchanging) variable, use const instead of let

바뀌지않는 변수 선언을 위해 let 대신 const를 쓴다.

Variables declared using const are called 'constants'. They cannot be reassigned. An attempt to do so would cause an error.

Uppercase constants

There is a widespread practice to use constants as aliases for difficult-to-remember values that are unknown prior to execution.

실행 전 기억하기 어려운 값의 별칭으로 constants를 사용하는 관행이 있다.

Such constants are named using capital letters and underscores.

이러한 constant는 대문자와 _를 사용하여 이름짓는다.

Name things right

A variable name should have a clean, obvious meaning, describing the data that it stores.

Some good-to-follow rules are:

  • Use human-readable names like userName or shoppingCart.
  • Stay away from abbreviations or short names like a, b, c, unless you really know what you're doing.
  • Make names maximally descriptive and concise.
  • Agree on terms within your team and in your own mind.

Using different variables for different values can even help the engine optimize your code.

0개의 댓글