JavaScript #1

Haebin Ethan Jeong·2020년 7월 20일
0
post-thumbnail

Difference between var, let, const

var

  • var can be redefined.

let

  • let can be given with different values.

const

  • const cannot be changed.

Math Expressions

let num = 1;
let newNum = num++;
console.log(num);
console.log(newNum);

num will return 2, and
newNum will return 1.

As num was assigned to newNum, newNum becomes 1. And, as num++ is called, num becomes 2.

Logical Operators

Using or (||)

Using and (&&)

Accepting parameter in a function

You can't assign a new value to the parameter in the function.

Function with multiple parameters

First Way - Very long and Inefficient

Better Way

Array

length of an array = myArray.length

Unlike python, in order to access the last element of an array, we need to use myArray[myArray.length - 1]

Manipulating the Array

  • push adds an element in the end of an array.
let month = [1, 2, 3, 4];
month.push(5);

>> month = [1, 2, 3, 4, 5]
  • unshift adds an element at the beginning of an array.
let month = [1, 2, 3, 4];
month.push(0);

>> month = [0, 1, 2, 3, 4]
  • pop removes the element of an array.
let month = [1, 2, 3, 4];
month.pop();

>> month = [1, 2, 3]

How to Check the Data Type

data = "string";
typeof data;

>> type - string

data = true;
typeof data;

>> type - boolean

Slicing an array

  • You can get the index of a certain word with indexOf
let word = "I am from the States.";

>> word.indexOF('m') == 3
  • slice(start index, end index)
let word = "I am from the States.";

>> word.slice(0,6) == "I am fr"
>> word.slice(7, word.length) == "om the States."
  • In order to slice to the end, we can use {string}.length
profile
I'm a Junior studying Economics and Computer Science at Vanderbilt University.

0개의 댓글