표현 대 진술

9946 단어
우리 프로그래머들은 Expression과 Statement 사이에서 많은 혼란을 겪고 있습니다. 이 기사의 목적은 짧은 개요를 통해 이 주제를 명확하게 하는 것입니다.

식과 명령문의 기본적인 차이점은 식은 하루가 끝날 때 무언가를 반환하고 데이터를 생성하며 한 곳에 저장할 수 있다는 것입니다. 그리고 이 명령문은 데이터를 생성하지 않으며 어디에도 저장할 수 없으며 아무 것도 반환하지 않습니다. 함수 정의는 문장이고 함수 호출은 표현식입니다. 함수를 작성할 때 함수가 호출될 때까지 아무 것도 반환하지 않기 때문입니다. 화살표 함수를 쓰면 변수에 저장되기 때문에 표현식이다. 몇 가지 예를 살펴보겠습니다.

const name1 = 'Rayhan'; // Statement
const name2 = 'Alvi'; // Statement
const name3 = 'Anik'; // Statement
const name4 = 'Arjun'; // Statement
const name5 = 'Ayman'; // Statement


이것들은 모두 진술입니다. 그들은 아무것도 반환하지 않기 때문입니다.

const students = [
    'Rayhan',
    'Alvi',
    'Anik',
    'Arjun',
    'Ayman',
    'Ayuub',
    'Bidyut',
]; // Statement

console.log(students[0]); // Expression
console.log(students[1]); // Expression
console.log(students[2]); // Expression
console.log(students[3]); // Expression
console.log(students[4]); // Expression

for (let i = 0; i < students.length; i++) {
    console.log(students[i], students[i].toLowerCase()); // Expression
} // Statement

students는 아무 것도 반환하지 않기 때문에 문입니다. console.log()는 무언가를 반환하기 때문에 표현식입니다. for 루프는 아무 것도 반환하지 않기 때문에 문이지만 for 루프 내부의 console.log(students[i], students[i].toLowerCase())는 표현식입니다.

function nameOfFunction(name) {
    if (!name) {
        console.log('Please provide your name');
    } else {
        console.log('Hello', name);
    }
} // Statement


이것을 함수문이라고 합니다. 호출될 때까지 아무 것도 반환하지 않기 때문입니다.

const nameOfFunction = function (name) {
    if (!name) {
        console.log('Please provide your name');
    } else {
        console.log('Hello', name);
    }
} // Expression


이것은 변수에 저장되기 때문에 함수 표현식이라고 합니다.

nameOfFunction('Murshed'); // Expression
nameOfFunction('Fahim'); // Expression
nameOfFunction(); // Expression


모든 함수 호출은 무언가를 반환하기 때문에 표현식입니다. 반환할 항목이 없으면 최소한 undefined 를 반환합니다.

function generateRandomNumber(min = 1, max) {
    const randomNumber = Math.floor(Math.random() * min + (max - min)); // Statement
    return randomNumber; // Expression
} // Statement

console.log(generateRandomNumber(5, 10)); // Expression


이것이 마지막 예입니다. 함수 정의 generateRandomNumber is statement, const randomNumber = Math.floor(Math.random() * min + (max - min)) is statement, Math.floor(Math.random() * min + (max - min)) is expression, return randomNumber is expression, console.log(generateRandomNumber(5, 10)) is expression.

표현과 진술의 개념을 명확히 하기 위해 간략한 개요를 제공하려고 노력했습니다. 당신이 이해할 수 있기를 바랍니다. 😊😊

좋은 웹페이지 즐겨찾기