javascript怎么求两个数的最大值
时间:2022-02-11 16:13
方法:1、利用if语句,语法“if(a>b){max=a;}else{max=b;}”;2、利用三元运算,语法“max=a > b ? a : b;”;3、利用Math对象的max()方法,语法“Math.max(a,b)”。 本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。 javascript求两个数最大值的方法 方法1:利用if语句(此种适合求两个数值大小时用) 输出: 方法2:利用三元运算来求 输出: 方法3:利用Math对象的max()方法 max() 方法可返回两个指定的数中带有较大的值的那个数。 【相关推荐:javascript学习教程】 以上就是javascript怎么求两个数的最大值的详细内容,更多请关注gxlsystem.com其它相关文章!var a=2,b=3;
var max;
if ( a > b ){
max=a;
}else{
max=b;
}
console.log(max);
var a=4,b=3;
var max;
max=a > b ? a : b;
console.log(max);
var a=4,b=6;
var max;
max=Math.max(a,b);
console.log(max);