Daha qısa JavaScript kodu yazmaq üçün 6 üsul.

Simuratli
1 min readAug 23, 2020

1. Ternary operator.

Uzun yol:

const num = 12;
let answer;
if(x>10){
answer = "10dan böyükdür"
}else {
answer = "10 dan kiçikdir"
}

Qısa yol:

const answer = x>10 ? "10dan böyükdür" : "10dan kiçikdir";

2.Dəyişkənlərin təyini.

Uzun yol:

let vegetable;
let fruit= "apple";

Qısa yol:

let vegetable,fruit="apple";

3.Gizli Return

Uzun yol:

function calcCircle(diametr){
return Math.pi*diametr
}

Qısa yol:

calcCircle = diametr => Math.pi * diametr;

4. Çox sətirli string

Uzun yol:

const multiLine = " Multi \n\t"
+ "line \n\t"
+ "string \n\t"

Qısa yol:

const multiLine = `Multi
line
string`

5. Arrow functions

Uzun yol:

function hello(){
console.log("hello")}
list.forEach(function(item){
console.log(item)
})

Qısa yol:

hello = () =>console.log("Hello");list.forEach(item => console.log("item"))

6. Template Literals

Uzun yol:

const welcome = “welcome ”+ fistname+ “ ” + lastname;const db = “http://”+host+ “:” + port+ “/” + database;

Qısa yol:

const welcome = `Welcome ${firstname} ${lastname}`;const db = `http://${host}:${port}/${database}`;

--

--

Simuratli
Simuratli

Written by Simuratli

MSc. High Energy and Plasma Physics | B.A. Computer Engineering | Content Creator. https://bento.me/simuratli

No responses yet