Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法混合BigInt和其他类型,请使用显式转换(我试图使用var添加/混合BigInt)_Javascript_Typeerror_Bigint - Fatal编程技术网

Javascript TypeError:无法混合BigInt和其他类型,请使用显式转换(我试图使用var添加/混合BigInt)

Javascript TypeError:无法混合BigInt和其他类型,请使用显式转换(我试图使用var添加/混合BigInt),javascript,typeerror,bigint,Javascript,Typeerror,Bigint,我尝试使用BigInt和add to sum来添加大的数字 var sum=0; for(let i in ar){ sum += BigInt(ar[i]); } return (sum); 但有一个错误是: sum += BigInt(ar[i]); ^ TypeError: Cannot mix BigInt and other types, use explicit conversions 我试着回答说,我

我尝试使用BigInt和add to sum来添加大的数字

var sum=0; 
    for(let i in ar){
        sum += BigInt(ar[i]);
    }
    return (sum);  
但有一个错误是:

sum += BigInt(ar[i]);
               ^

TypeError: Cannot mix BigInt and other types, use explicit conversions

我试着回答说,我们不能把BigInt和其他类型混合在一起。所以我把整数和转换成BigInt,然后把它加到BigInt。 如在“中所说”https://javascript.info/bigint“:

警报(1n+2);//错误:无法混合BigInt和其他类型

设bigint=1n; 设数=2

//到bigint的数字 警报(bigint+bigint(数字));//三,

//比数 警报(数字(bigint)+数字);//3. 因此,我现在的工作解决方案是:

var sum=0
for(let i in ar)
    sum = BigInt(sum) + BigInt(ar[i]);
return (sum);