Function and loop in JS
Tags: javascript, loop, function
Published: 2024-06-03 08:46:58

## Function
**JavaScriptda funksiyalarni e'lon qilishning bir necha usullari bor. Asosiy usullari quyidagilar:**
- Function Declaration
- Function Expression
- Arrow Function 

**Har bir usulning o'ziga xos afzalliklari va qo'llanish joylari bor. Keling har birini misollar bilan tushuntirib beraman.**

_1. Function Declaration_

_Funksiya e'lon qilish - bu funksiyani o'z e'lon qilingan joyidan oldin ham chaqirish mumkin._

```
// Funksiya e'lon qilish
function greet(name) {
    return `Hello, ${name}!`;
}

// Funksiyani chaqirish
console.log(greet("Mirabbos")); // Hello, Mirabbos!
```

_2. Function Expression (Funksiya ifodasi)_

_Function Expression - bu funksiyani o'zgaruvchiga tenglashtirish orqali aniqlash usulidir. Bunda funksiya o'z e'lon qilinganidan keyin chaqirilishi mumkin._

```
// Funksiya
const greet = function(name) {
    return `Hello, ${name}!`;
};

// Funksiyani chaqirish
console.log(greet("Mirakbar")); // Hello, Mirakbar!
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qatw8uohaknvw8mzui7z.png)



_3. Arrow Function (Strelka funksiyasi)_

_Arrow funksiyasi - bu qisqa va soddalashtirilgan funksiyani yozish usulidir. Ular function kalit so'zidan foydalanmaydi va kontekstni (this qiymatini) o'zgarishiga imkon bermaydi, shuning uchun this boshqa o'zgaruvchilarga ko'ra boshqacha ishlaydi._

```
// Arrow funksiyasi
const greet = (name) => {
    return `Hello, ${name}!`;
};

// Yana ham qisqa ko'rinish
const greetShort = name => `Hello, ${name}!`;

// Funksiyani chaqirish
console.log(greet("Sevara")); // Hello, Sevara!
console.log(greetShort("Mirabkar")); // Hello, Mirabkar!
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8osoge7tf2vj6je35j5u.png)



## Loop

**JavaScriptda bir nechta turdagi looplar mavjud bo'lib, ular kodni qayta-qayta bajarishga imkon beradi. Asosiy looplar quyidagilar:**
- `for` loop
- `while` loop
- `do while` loop
- `for in` loop
- `for of` loop

**Har birini misollar bilan ko'rib chiqamiz.**

_1. for Loop_

_for loop kiritilgan shart bajarilgan vaqtgacha qayta-qayta ishlaydi._

```
// 1 dan 5 gacha bo'lgan sonlarni chop etish
for (let i = 1; i <= 5; i++) {
    console.log(i);
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kpjgrytltroiifsv1gi.png)



_2. while Loop_

_while loop berilgan shart true bo'lganda bajariladi._

```
// 1 dan 5 gacha bo'lgan sonlarni chop etish
let i = 1;
while (i <= 5) {
    console.log(i);
    i++;
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0vfec6d54ma0pbqm3rc.png)



_3. do while Loop_

_do while loop kamida bir marta bajariladi va keyin shart true bo'lsa qayta bajariladi._

```
// 1 dan 5 gacha bo'lgan sonlarni chop etish
let i = 1;
do {
    console.log(i);
    i++;
} while (i <= 5);
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rx6tuboxry5w2l4vr6n.png)



_4. for in Loop_

_for in loop obyektdagi barcha o'zgaruvchilar (properties) orqali aylanish uchun ishlatiladi._

```
// Obyekt ichidagi barcha keylar
const person = {
    name: "Mirabbos",
    age: 25,
    city: "Tashkent"
};

for (let key in person) {
    console.log(key + ": " + person[key]);
}
```


_5. for of Loop_

_for of loop iteratsiya qilinadigan (array, string) obyektlarning elementlari orqali aylanish uchun ishlatiladi._

```
// Array ichidagi barcha elementlarni chop etish
const numbers = [1, 2, 3, 4, 5];

for (let number of numbers) {
    console.log(number);
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53mbk5jqjy1nova7ojzj.png)