01_JavaScript : Variables & Operators

01_JavaScript : Variables & Operators

console.log / let / const / operators / Merge Arrays / Ternary operators

·

3 min read

Table of contents

No heading

No headings in the article.

Vanilla JavaScript : pure JavaScript without any libraries

  1. Chrome Inspect

  2. Codesandbox (https://codesandbox.io)


Ex01. console.log();

console.log(1+2+3+4);
console.log('Hello');

Ex02. let

let value = 1; 
console.log(value); // 1
value = 2;
console.log(value); // 2

(* we don't use var any more)

Ex03. const

const a = 1;
a = 2; // Error

Ex04. Number / String / Boolean / Null / Undefined

let value = 1; // Number
let text = 'hello'; // String (Double or single quotation marks) 
let good = true; // boolean
const friend = null // null : literally empty on purpose
let criminal // undefined

Ex05. Arithmetic Operators

let a = 1 + 2;
console.log(a); //3
a++;
++a;
console.log(a); //5
console.log(a++); //5 -> 6 on the next line
console.log(++a); //7

Ex06. Assignment Operators

let a = 1;
a = a + 3;
let b = 5;
b += 3;
b -= 3;
b *= 3;
b /= 3;

Ex07. Boolean Operators

  • ! : Not

  • && : AND

  • || : OR

const a = !true; //false
const b = !false; //true

const c = true && false //false
const d = true || false //true

const value = !((true && false) || (true && false) || !false); // left to right 
// !(false || false || true) 
// !(true)
// false

Ex08. Comparison Operators(1)

  • ==

  • === : Type Check! (*recommendation)

  • !=

  • !== : Type Check!

const a = 1;
const b = '1';
const equals_01 = a == b; //true
const equals_02 = a === b; //false
const a = 0;
const b = false;
const equals = a == b; // true : 0 and false are same here
const a = null;
const b = undefined;
const equals = a == b; // true : null and undefined are same here
const value = 'a' !== 'b'; // true
console.log(1 != '1'); // false
console.log(1 !== '1'); // true

Ex09. Comparison Operators(2)

  • <, >, <=, >=
const a = 10;
const b = 15;
const c = 15;

console.log(a < b); // true
console.log(b > a); // true
console.log(b >= c); // true
console.log(a <= c); // true
console.log(b < c); // false

Ex10. (concatenate) Merge Arrays

  • str1 + str2
const a = 'Hi, ';
const b = 'This is Dana';
console.log(a+b); //Hi, This is Dana

Ex11. Ternary Operators

const array = [];
let text = array.length === 0 ? '배열이 비어있습니다' : '배열이 비어있지 않습니다.';
// you can directly assign 
console.log(text);

cf. https://learnjs.vlpt.us/basics/02-variables.html