korean

Data Type

JavaScript primitives consist of strings, numbers, and booleans. Use the const, let, or var keyword when declaring a variable. Prefer const and let over var as they provide block-level scoping. Use const by default, and only use let when reassignment is necessary. Avoiding var is a recommended best practice in modern JavaScript.

const city = "New York";//or 'New York'
const result = 1200 / 1000;
const onGlass = true;
alert('city: ' + city);
alert('result: ' + result);
alert('onGlass: ' + onGlass);
let sum = 0;
for (let i = 1;i <= 10;i++) {
	sum += i;	
} 
alert('Sum from 1 to 10: ' + sum);

The following error occurs when sum is declared as a const.
TypeError: Assignment to constant variable.

Manipulating array elements is not the same as reassigning the array variable. The code below is adding elements to an array declared with const."

const years = new Array(1969, 1970);
years[57] = 2026;
alert("years.length=" + years.length);
alert("years=" + years);
for (let idx in years) {
    alert(years[idx]);
}

The following is an example of array variable reassignment.

const regExp = new RegExp('java', 'gi'); // /java/gi
const testStr = "https://java-school.net is the best site to learn Java";
let retArr = regExp.exec(testStr);
alert(retArr[0]);
retArr = regExp.exec(testStr);
alert(retArr[0]);

RegExp.prototype.exec() returns an array.
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec

Global Variable and Local/Block Variable

Global Variable: A variable declared in the outermost scope of the code, which can be accessed and modified globally throughout the application.

Local/Block Variable: A variable declared within a function or a curly brace {} block, accessible only inside the block where it is defined.

const a = 2;//global variable
function test() {
	const a = 1;//local/block variable
	alert(a);
}
test();
alert(a);

Comment

"// is used for single-line comments, while /* ... */ is used for multi-line comments."

Identifier

You can combine letters, numbers, _, and $ to create an identifier, which must not have the same name as any keyword. JavaScript identifies the case of identifiers.

undefined

The undefined means 'no value assigned.' You get this when you refer to an uninitialized variable.

var i;
alert('i: ' + i);
let i;
alert('i: ' + i);

This code generates an 'Uncaught SyntaxError: missing = in const declaration' error.

const i;
alert('i: ' + i);

Type Conversion

"0, NaN, null, undefined, and empty strings are falsy values that evaluate to false in a boolean context."

var i;
if (i) {
  alert('i: ' + "true");
} else {
  alert('i: ' + "false");
}

This code generates an 'Uncaught SyntaxError: missing = in const declaration' error.

const i;
if (i) {
  alert('i: ' + "true");
} else {
  alert('i: ' + "false");
}

Equality Operator and Strict Equality Operator

  • == (Equality Operator): Performs type coercion (loose equality).
  • === (Strict Equality Operator): Checks type and value without conversion (strict equality).
if ('7' == 7) {
    alert("true");
} else {
    alert("false");
}
if ('7' === 7) {
    alert("true");
} else {
    alert("false");
}

In JavaScript, the code null == undefined returns true because of the Abstract Equality Comparison Algorithm (loose equality ==), which treats both values as equivalent representations of "no value".

if (null == undefined) {
	alert("true");
} else {
	alert("false");
}

In JavaScript, the type of null is 'object', while the type of undefined is 'undefined' itself. Because they have different types, === returns false.

if (null === undefined) {
	alert("true");
} else {
	alert("false");
}

In JavaScript, implicit type coercion is when the JavaScript engine automatically converts a value from one data type to another, even if the developer does not intentionally do so.

alert("20" + 30);
alert("April 1, " + 2026);
alert("20" - 30);
alert("20" * 30);
alert("20" / 30);
alert("20" - 30);
alert("20" + 30);//String concatenation operators have higher precedence than arithmetic operators.
if ("20" > 30) {
	alert("true");
} else {
	alert("false");
}

Explicit type conversion uses the String(), Number(), and Boolean() functions.

alert(String(20) + '30');
alert("April 1, " + String(2026));
alert("April 1, " + (2026).toString());
alert(Number("20") * 30);
alert(Number("20") / 30);
alert(Number("20") - 30);
alert(Number("20") + 30);
alert(Number("3.14"));
alert(parseInt("3.14"));
alert(parseInt("10px"));