javascript - Comparison operators !== against 0 -
i think obvious code does.
why code return whole string if use !== operator? know arrays in javascript start @ index 0, , here i'm entering whole filename argument, indexof(".") greater 0. no, i'm not passing .htaccess file here.
function getfileextension(i) { // return file extension (with no period) if has one, otherwise false if(i.indexof(".") !== 0) { // return i.slice(i.indexof(".") + 1, i.length); } else { return false; } } // here go! given filename in string (like 'test.jpg'), getfileextension('pictureofmepdf'); return given string // both operand same type , value but if change comparasion
(i.indexof(".") > 0) // logs false p.s. case asking, form usvsth3m.
indexof() returns index of substring, can return 0, mean substring appears @ position 0. if substring not found, returns -1 instead, change if statement reflect logic:
if(i.indexof(".") >= 0) additionally, should use substring() extract substring string - slice() arrays.
return i.substring(i.indexof(".") + 1, i.length); still, think better way split():
var filenamearray = i.split("."); // "foo.txt" --> ["foo" "txt"] if(filenamearray.length >= 2) { return filenamearray[1]; } else { return false; //maybe want return "" instead? }
Comments
Post a Comment