variables

chloe·2020년 8월 21일
1

Javascript

목록 보기
8/13

>variable type

primitive? single item :number, string,boolean,null,undefined,symbol
primitive의 경우 값(value)자체가 메모리에 저장된다
Object? box container 오브젝트는 너무커서 메모리에 한번에 올라갈 수 없다.
오브젝트가 가르키는 레퍼런스ref가 메모리에 저장된다.
이 둘에 따라 메모리에 다른 방식으로 저장된다.

데이터타입

  1. Immutable data types(변경불가) :primitive types,frozen objects(object.freeze())
    2.Mutable data types(변경가능) : all objects by defauly are mutable in JS

Create a Variable:var

Var myName='chloe';
console.log(myName);
//Output:chloe

1.Var,is a JavaScript keyword that creates, or declares, a new variable.
2.myName, is the variable's name.
3.Chloe is the value assigned(=)to the variable myName.

There are a few general rules for naming variables.

  • Variable names cannot start with numbers.
  • Variable names are case sensitive, so myName and myname would be different variables.

Let

The let keyword signals that the variable can be assigned a different value.

let meal='Enchiladas';
 console.log(meal);//
 Output:Enchiladas
meal='Burrito';
console.log(meal);//
output:Burrito

We can declare a variable without assigning the variable a value. In such a case, the variable will be automatically initialized with a value of undefined:

let price;
console.log(price);//
Output:undefined
price=350;
console.log(price);//
Output:350

Const

Const variable cannot be reassigned because it is constant. If you try to reassign a const variable, you'll get a TypeError.
Constant variables must be assigned a value when declared.
If you try to declare a const variable without a value you'// get a SyntaxError.

Mathematical Assignment Operators

let s =4;
s=s+1;
console.log(s);//output:5

+=,-=,/=
let x=20;
x-=5;//x=x-5
console.log(x);//Output:15

let y =50;
y=2;//
y=y
2
console.log(y);// Output:100

let z=8;
z/=2;// Z=Z/2
console.log(z);//4

The Increment and Decrement Operator

Increment Operator(++)

Decrement Operator(--)

String interpolation

Check out the following example where a template literal is used to log strings together

const myPet='cat';
console.log(`I own a pet${myPet}.`);
Output: I own a pet cat.
  • Template literal is wrapped by backticks(`)

  • Inside the template literal, you'll see a placeholder,${myPet}.
    The value of myPet is inserted into the template literal.

One of the biggest benefits to using template literals is the readability of the code. Using template literals, you can more easily tell what the new string will be.

Typeof operator

If you need to check the data type of a variable's value, you can use the typeof operator.
The typeof operator checks the value its right and returns.

const unknown1='foo'
console.log(typeof unknown1); output: string

const unknown2=10;
console.log(typeof unknown2)// Output: number

const unknown3=true;
console.log(typeof unknown3)// Output:boolean

참고:codecademy

profile
Front-end Developer 👩🏻‍💻

0개의 댓글