JavaScript Essentials: Type, Scope, Functions, and More

안동현·2025년 2월 4일

Frontend

목록 보기
3/4
post-thumbnail

What is JavaScript?

JavaScript is a scripting language that runs in web browsers and is used to add dynamic functionality to web pages.
Since ES6 (ECMAScript 2015), JavaScript has introduced many new features, making programming more powerful and efficient.


Key Features

  • Dynamic typing: No need to declare variable types.
  • Prototype-based object-oriented programming: Objects can be created and inherited without using classes.
  • Functional programming: Functions are treated as first-class objects.
  • Event-driven programming: Reacts to user interactions.

Running JavaScript with Node.js

To execute a JavaScript file in a Node.js environment, use the following command in the terminal:

node index.js
  • Node.js must be installed.
  • The index.js file must exist in the current directory.

Basic Syntax

Variable Declaration

KeywordScopeRedeclarationReassignment
varFunction or globalYesYes
letBlockNoYes
constBlockNoNo
var x = 10;
var x = 20; // Allowed
x = 30;     // Allowed

let y = 10;
y = 30;     // Allowed

const z = 10;
// z = 20;  // Error: Cannot reassign

Semicolons and Comments

  • Semicolons (;): Optional but recommended for clarity.
  • Comments:
// Single-line comment
let x = 10; // Variable declaration

/*
Multi-line comment
Declaring and initializing variable y.
*/
let y = 20;

Logging Output

console.log("Hello, World!"); // Outputs a string

let a = 5;
let b = 10;
console.log(a + b); // 15

Scope and First-Class Functions

Scope

JavaScript variables and functions have different levels of accessibility:

  • Global Scope: Accessible anywhere in the code.
  • Function Scope: Available only within the function.
  • Block Scope: Restricted to {} when using let and const.

First-Class Functions

Functions in JavaScript are first-class objects:

const sayHello = function() {
    console.log("Hello!");
};

function executeFunction(func) {
    func();
}

executeFunction(sayHello); // "Hello!"

Hoisting

Hoisting refers to JavaScript moving variable and function declarations to the top of their scope.

console.log(x); // undefined
var x = 5;

console.log(y); // Error: Cannot access 'y' before initialization
let y = 10;

function sayHello() {
    console.log("Hello!");
}
sayHello(); // "Hello!"
  • var variables are hoisted and initialized as undefined.
  • let and const are hoisted but not initialized (Temporal Dead Zone).

Data Types & Operators

Data Types

Primitive Types

let num = 42;       // Number
let name = "Alice"; // String
let isActive = true; // Boolean
let value;          // undefined
let empty = null;   // Null
let sym = Symbol("id"); // Symbol
let bigNumber = 123456789012345678901234567890n; // BigInt

Reference Types

let person = { name: "Alice", age: 25 }; // Object
let numbers = [1, 2, 3]; // Array

Operators

Arithmetic Operators

console.log(5 + 3); // 8
console.log(10 - 4); // 6
console.log(6 * 7); // 42
console.log(10 / 2); // 5
console.log(10 % 3); // 1
console.log(2 ** 3); // 8

Comparison Operators

console.log(5 == "5");  // true (loose equality)
console.log(5 === "5"); // false (strict equality)
console.log(5 != "5");  // false (loose inequality)
console.log(5 !== "5"); // true (strict inequality)
console.log(10 > 5);    // true
console.log(5 <= 5);    // true

Logical Operators

console.log(true && false); // false (AND)
console.log(true || false); // true (OR)
console.log(!true);         // false (NOT)

Ternary Operator

let age = 18;
let status = (age >= 18) ? "Adult" : "Minor";
console.log(status); // Adult

Type Conversion Operators

console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"

let arr = [];
console.log(arr instanceof Array); // true

Conclusion

JavaScript is a versatile language used in both front-end and back-end development.
Understanding the fundamentals is essential for writing efficient and maintainable code.


profile
내가 깨달은 것과 내가 리마인드할것

1개의 댓글

comment-user-thumbnail
2025년 5월 27일

Great! Many useful features of the program bring the best exploitation. Build and develop the program to practice application soccer random skills. Follow the article topics to improve professional skills.

답글 달기