Hanachoi 2022. 7. 30. 15:50

#Primitive types

  • Every language has their own type of information. Js has things to save some information with these basic building blocks. > numbers / string / boolean / null / undefined (these are basic primitive type in Js)
  • REPL :  Read my code / Evaluate / Print out the result  / Loop(keep going) : Js doesn't stop compare to most other programming languages. You type some code and hit run and once it's done

 

#Numbers

  • You usually need to use numbers in Js so much. When you make a button which when user hit the button, is going to increase number like Like amount. 
  • In JS, it has one number type. Other languages usually have more numbert types like Integer and then a seperate type to represent decimal numbers called float points or float's often. Because it takes a lot more memory to store a decimal. 
  • Numbers in Js include positive number / negatives numbers / whole numbers(integer) / decimal
  • Numbers get a certain amount of space in memory. For example, when you type 1.999999999999 and then add one more 9 at the end, It will be considered as 2. They are not permitted just to take up an infinite amount of space, infinite surround term, but there is a hard limit .
  • There is an order of operations  : PEMDAS : parentheses runs first, and then Exponent(지수), multiplication(곱하기), division , addition, substraction(빼기)
  • Modulo : It is a remainder operator. We have nine mode two, two goes into nine. How many times as a whole number? It goes in four times and then what is the remainder left after that ? Four times two is eight, so That's a remainder of one. nine minus eight. 
  • Modulo is commonly used to determine if a number is even or odd. We can take any numer and mod by two, and Mod by two and that will tell us if it;s even or odd. If we get one, there's a remainder of one, after tryting to divide tow into this number, that means it's odd. Also if it's an even number, we'll get a remainder of zero. 
9 % 2  // 1, % is called Mode/ modulo
19 % 4 // 3
217637 % 2 // 1 : able to know this numeber is odd
  • exponent(지수 연산자) :  two multiplication, scienter , two asterisks , two stars and it's just going to take our first number and raise it to some power. 
2**4 // 16

 

 

#NaN

  • Nan is a numeric value that represents something that is not a number. 
  • In JS, we have [typeof] which is considered operator like + or - sign. In order to operate it, we need one more thing such as [ typeof 4 ] and then you can get reseult what it is. In this case, you can get "number" on console. 
  • So, if you type [ typeof NaN],  you can get "number".

 

#Variables

  • Variables are like labels for values. we can store a value and give it a name so that we can : refer back to it later / use that value to do or chang it later one.
//basic syntax
let someName = value;
  • make me a variable called 'year' and give it the value of 1985. 
let year = 1985;
  • It is basically just registering a variable. The simplest thing we could do is just recall it. Everytime I refer to a year, I am getting 1985. 
  • You can just recall a value that variable has. It doesn't change its value. 
let numHens = 5;
let numRoosters = 1;

numHens + numRoosters // 6

let totalChickens = numHens + numRooster  // 6
totalChicekns + 1 //7

numHens = 6 ;// numHens의 숫자를 바꿧다고 해서 totalChicken 안에 들어있는 숫자도 자동으로 바뀌지는 않는다.  수동으로 바꿔줘야 함.

 

 

# Variables update

  • In order to update number, you can use operators to increment or decrement.
  • The most common operators are  ++ / -- to increment or decrement by one.
let numLives = 9;

numLives++; // 10
numLives--; // 8


numLives += 10; // 19
numLives -=9; // 0
numLives *=2 //18
numLives /=3; //3

 

 

# Const / Var

  • Const stands for Constant. Constant is the value that doesn't change. It reamains constant. When you try to change any variables that is made by const, you can not change the value. Constant variable is not allowed to change or updeate the value. can not be reassigned. 
  • After you learn about Arrays & objectsm, you can see other situations where const makes sense over let. 
  • Var : It was the only way to make variables before Let / const. These days, there isn't really a reason to use it. But still working.

 

# Boolean

  • Boolean is simple. We have two options, true and false for boolean value. 
  • Boolean is used to store Yes / NO value and True/ False. 
  • You can get a lot of information with Boolean value. Such as "is this game still playing?" / "is this user logged in?".
  • You sould use only small letters for true/ false, otherwise, you will get error message.
  • In JS, you can have a single variable that changes its type. 
let isActiveGame = false; // boolean value
isActiveGame = 1; // change false to 1(number)
  • In hightlight that, in other languages, you cannot change its value. If you already make a variable have a number value, it won't be changed. This variable only ever can store number value. 

 

# Nameing variables and rules.

  • let isLoggedIn => you can creat an identifier. let with a variable name. 
  • No space In variable name.
  • You cannnot start with a degit. you can use number but should not be the first charactor.  
  • In JS, indentifiers are case-sensitive and can contain Unicode letters $ and _. You can start with _ for first charactor but it is not that common. 
  • Usually in JS, we use camleCase a lot which is each new one is uppercase excep for the very first charactor. You can also use snake_case but not really common. 
  • It is imporatant to make a good name for variable. You can name a variable like just n. Of course you are able to know what it is for right now, but later you may not remember what it is for. Also when others see your functions or variables, it will make people get confused. 
  • When you make any boolean variables, it is good for use is at the very first. [ Ex) let isLoggedin / let isGameOver] It can make it more clearer it is boolean value. 
  • tend to avoid one letter variable name.