Note JavaScript Basic Point

sunghoon·2025년 1월 3일
0

2.0 Glove Project

목록 보기
7/35
post-thumbnail

2.18 JavaScript specials

code structure

// statements are delimited with a semicolon
alert('Hello'); alert('World');
// Usually, a line-break is also treated as a delimiter
alert('Hello')
alert('World')
// That’s called “automatic semicolon insertion”.
alert("There will be an error after this message")

[1, 2].forEach(alert) 

// TypeError: Cannot read properties of undefined (reading '2')
// Semicolons are not required after code blocks {...}
function f() {}
for (;;) {}
// …But even if we can put an “extra” semicolon somewhere, that’s not an error. It will be ignored.

Strict mode

'use strict';
// the directive must be at the top a script or at the beginning of a function body

Variables

  • let
  • const : constant, can’t be changed
  • var: old style
  • data types
    • number: for both floating-point and integer numbers
    • bigint: for integer numbers of arbitrary length,
    • string: for strings,
    • boolean: for logical value: true/false
    • null - a type with a single value undefined,
    • undefined: a type with a single value undefined, meaning
      ”not assigned”,
    • object and symbol: for complex data structures and unique identifiers, we haven’t learnt them yet.
  • The typeof opertor returns the type for a value, with two exceptions:
typeoff null == "object" // error in the language
typeof function() {} == "function" // functions are treated specially

Interaction

prompt(quesion, [default]);
// to show null if they clicked "cancel"
confirm(question);
// return true or false
alert(message);
// Output a message

operators

  • Arithmetical: * + - / % **
  • Assignments: =
  • Bitwise: 32-bit integers at the lowest (| & ^ …)
  • conditional check ? A : B
  • Logical operators && || !
  • Nullish coalescing operator a ?? b
  • Comparisons ==
    strict equality operator ===
alert( 0 == falsee ); // true
alert( 0 == ''); // true

Loops

/* 3 types of loops */

//  1
while (condition) {
	...
}

// 2
do {
	...
}while ();

// 3
for (let i = 0; i < 10 ; i++) {
	...
}
  • break/continue

The “switch” construct

let age = prompt('Your age?', 18);

switch (age) {
	case 18:
		alert("Won't work"); // the result of 
		break;
	case "18":
		alert("This works!");
		break;
		
	default:
		alert("Any value not equal to one above");
}

Functions

// 1. Function Declaration
function sum(a, b) {
	let result = a + b;
	
	return
}
// 2. Function Expression
let sum = function(a, b) {
	let result = a + b;
	
	return result;
};
// 3. Arrow functions

//expression on the right side
let sum = (a, b) => a + b;

// or multi-line syntax with { ... }, need return here:
let sum = (a, b) => {
	// ...
	return a + b;
}

// without arguments
let sayHi  = () => alert("Hello");

// with a single argument
let double = n => n * 2;
profile
프라다 신은 빈지노와 쿠페를 타는 꿈을 꿨다.

0개의 댓글