JavaScript

Type coercion

Në shqip: Konvertim automatik i tipit

ShpjegimiSQ

Type coercion është kur JavaScript-i e ndryshon vetë tipin e një vlere pa ta kërkuar ti — p.sh. "5" + 1 jep "51" (tekst), por "5" - 1 jep 4 (numër). Është shkaku i shumë bug-eve të çuditshme.

EnglishEN

Type coercion is when JavaScript changes a value's type on its own without you asking — e.g. "5" + 1 gives "51" (text), but "5" - 1 gives 4 (a number). It's behind lots of strange bugs.

Si ta mendosh

Si kur i thua gjyshes „më sill pesë“ dhe ajo të sjell pesë mollë, ndërsa ti doje pesë euro: e „kuptoi“ vetë, por jo si e doje ti.

Lexoje në anglisht

Like asking grandma to “bring five” and she brings five apples when you meant five euros: she “figured it out” herself, just not the way you meant.

Shembull kodijavascript

console.log("5" + 1);          // "51" (bashkim teksti)
console.log("5" - 1);          // 4    (zbritje numrash)
console.log(Number("5") + 1);  // 6    ← konvertim i qëllimshëm

Terma të lidhur