how2j.cn

关键字 简介 示例代码
==
!=
>
>=
<
<=
基本逻辑运算符
示例代码
===
!==
绝对等,绝对不等于
示例代码
?:
三目运算符
示例代码
示例 1 : 基本逻辑运算符   
示例 2 : 绝对等,绝对不等于   
示例 3 : 三目运算符   

示例 1 :

基本逻辑运算符

基本逻辑运算符 包含如下 :
==
!=
>
>=


返回一个Boolean类型的值,true或者false
"); window.frames["iframe_show1103"].document.write(decodeHtml($("textarea#stepcodeTextarea1103").val())); window.frames["iframe_show1103"].document.close(); $(window.frames["iframe_show1103"]).load(function(){ $("#iframe_show1103").height($("#iframe_show1103").contents().find("body").height()+showittryitheight); }); $("#iframe_show1103").height($("#iframe_show1103").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function p(s){ document.write(s); document.write("<br>"); } p("1==2: "+(1==2)); p("1!=2: "+(1!=2)); p("1>2: "+(1>2)); p("1<2: "+(1<2)); p("1>=2: "+(1>=2)); </script>
<script>
function p(s){
  document.write(s);
  document.write("<br>");
}

p("1==2: "+(1==2));
p("1!=2: "+(1!=2));
p("1>2: "+(1>2));
p("1<2: "+(1<2));
p("1>=2: "+(1>=2));

</script>
"); window.frames["iframe1103"].document.write(decodeHtml(code1103)); window.frames["iframe1103"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1103"]).load(function(){ $("#iframe1103").height($("#iframe1103").contents().find("body").height()+showittryitheight); }); $("#iframe1103").height($("#iframe1103").contents().find("body").height()+showittryitheight); alreadyWriteCode1103 = code1103; $("#rendering1103").hide(); $("#rendered1103").show(); } var tRereshRetry2DemoPanel1103 = setInterval(rereshRetry2DemoPanel1103,1000); var binded1103 = false; $("textarea#stepcodeTextarea1103").keyup(function(){ if(!binded1103){ $(window).bind('beforeunload',function(){ binded1103 = true; return "xxxx"; }); } var newCode = $(this).val() code1103 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1103!=newCode){ // window.frames["iframe1103"].document.write("
"); // window.frames["iframe1103"].document.write(decodeHtml($("textarea#stepcodeTextarea1103").val())); // window.frames["iframe1103"].document.close(); // $(window.frames["iframe1103"]).load(function(){ // $("#iframe1103").height($("#iframe1103").contents().find("body").height()+showittryitheight); // }); // code1103 = newCode; // } }); $(".tryButton1103").click(function(){ $("#tryDiv1103").show(); $("#stepcodeTextarea1103").focus(); $("#stepcodeTextarea1103").height(200); $("#iframe1103").height(0); window.frames["iframe1103"].document.write("
"); window.frames["iframe1103"].document.write(decodeHtml($("textarea#stepcodeTextarea1103").val())); window.frames["iframe1103"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1103"]).load(function(){ $("#iframe1103").height($("#iframe1103").contents().find("body").height()+showittryitheight); }); $("#iframe1103").height($("#iframe1103").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1103.focus(); editor1103.setSize(null, "250"); $("#rendering1103").hide(); $("#rendered1103").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1103 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1103"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1103.on("change",function(doc){ if(!binded1103){ $(window).bind('beforeunload',function(){ binded1103 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1103 = newCode; $("textarea#stepcodeTextarea1103").val(newCode); if(alreadyWriteCode1103!=code1103){ lastModifedTime1103 = new Date().getTime(); $("#rendering1103").show(); $("#rendered1103").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1103 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1103.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1103").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

绝对等,绝对不等于

与==进行值是否相等的判断不同 ,绝对等 ===还会进行 类型的判断
比如 数字1和 字符串'1'比较,值是相等的,但是类型不同
所以==会返回true,但是===会返回false
绝对不等于!== 与上是一个道理
"); window.frames["iframe_show1104"].document.write(decodeHtml($("textarea#stepcodeTextarea1104").val())); window.frames["iframe_show1104"].document.close(); $(window.frames["iframe_show1104"]).load(function(){ $("#iframe_show1104").height($("#iframe_show1104").contents().find("body").height()+showittryitheight); }); $("#iframe_show1104").height($("#iframe_show1104").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function p(s){ document.write(s); document.write("<br>"); } p("1=='1': "+(1=='1')); p("1==='1': "+(1==='1')); </script>
<script>
function p(s){
  document.write(s);
  document.write("<br>");
}

p("1=='1': "+(1=='1'));
p("1==='1': "+(1==='1'));

</script>
"); window.frames["iframe1104"].document.write(decodeHtml(code1104)); window.frames["iframe1104"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1104"]).load(function(){ $("#iframe1104").height($("#iframe1104").contents().find("body").height()+showittryitheight); }); $("#iframe1104").height($("#iframe1104").contents().find("body").height()+showittryitheight); alreadyWriteCode1104 = code1104; $("#rendering1104").hide(); $("#rendered1104").show(); } var tRereshRetry2DemoPanel1104 = setInterval(rereshRetry2DemoPanel1104,1000); var binded1104 = false; $("textarea#stepcodeTextarea1104").keyup(function(){ if(!binded1104){ $(window).bind('beforeunload',function(){ binded1104 = true; return "xxxx"; }); } var newCode = $(this).val() code1104 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1104!=newCode){ // window.frames["iframe1104"].document.write("
"); // window.frames["iframe1104"].document.write(decodeHtml($("textarea#stepcodeTextarea1104").val())); // window.frames["iframe1104"].document.close(); // $(window.frames["iframe1104"]).load(function(){ // $("#iframe1104").height($("#iframe1104").contents().find("body").height()+showittryitheight); // }); // code1104 = newCode; // } }); $(".tryButton1104").click(function(){ $("#tryDiv1104").show(); $("#stepcodeTextarea1104").focus(); $("#stepcodeTextarea1104").height(200); $("#iframe1104").height(0); window.frames["iframe1104"].document.write("
"); window.frames["iframe1104"].document.write(decodeHtml($("textarea#stepcodeTextarea1104").val())); window.frames["iframe1104"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1104"]).load(function(){ $("#iframe1104").height($("#iframe1104").contents().find("body").height()+showittryitheight); }); $("#iframe1104").height($("#iframe1104").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1104.focus(); editor1104.setSize(null, "250"); $("#rendering1104").hide(); $("#rendered1104").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1104 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1104"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1104.on("change",function(doc){ if(!binded1104){ $(window).bind('beforeunload',function(){ binded1104 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1104 = newCode; $("textarea#stepcodeTextarea1104").val(newCode); if(alreadyWriteCode1104!=code1104){ lastModifedTime1104 = new Date().getTime(); $("#rendering1104").show(); $("#rendered1104").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1104 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1104.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1104").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

三目运算符

三目运算符 ?: 有三个操作数
如果第一个返回true,就返回第二个操作符
否则就返回第三个操作符。
"); window.frames["iframe_show1105"].document.write(decodeHtml($("textarea#stepcodeTextarea1105").val())); window.frames["iframe_show1105"].document.close(); $(window.frames["iframe_show1105"]).load(function(){ $("#iframe_show1105").height($("#iframe_show1105").contents().find("body").height()+showittryitheight); }); $("#iframe_show1105").height($("#iframe_show1105").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var age = 15; var movie = age<18?"卡通":"你懂的"; document.write('使用?: 三相运算法进行判断。 <br>'); document.write('age<18?"卡通":"你懂的" <br>表示当年纪小于18的时候,就看卡通,否则就看 你懂得<br>'); document.write('而age变量的值是15,所以返回 '+movie); </script>
<script>

var age = 15;

var movie = age<18?"卡通":"你懂的";

document.write('使用?: 三相运算法进行判断。 <br>');
document.write('age<18?"卡通":"你懂的" <br>表示当年纪小于18的时候,就看卡通,否则就看 你懂得<br>');

document.write('而age变量的值是15,所以返回 '+movie);

</script>
"); window.frames["iframe1105"].document.write(decodeHtml(code1105)); window.frames["iframe1105"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1105"]).load(function(){ $("#iframe1105").height($("#iframe1105").contents().find("body").height()+showittryitheight); }); $("#iframe1105").height($("#iframe1105").contents().find("body").height()+showittryitheight); alreadyWriteCode1105 = code1105; $("#rendering1105").hide(); $("#rendered1105").show(); } var tRereshRetry2DemoPanel1105 = setInterval(rereshRetry2DemoPanel1105,1000); var binded1105 = false; $("textarea#stepcodeTextarea1105").keyup(function(){ if(!binded1105){ $(window).bind('beforeunload',function(){ binded1105 = true; return "xxxx"; }); } var newCode = $(this).val() code1105 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1105!=newCode){ // window.frames["iframe1105"].document.write("
"); // window.frames["iframe1105"].document.write(decodeHtml($("textarea#stepcodeTextarea1105").val())); // window.frames["iframe1105"].document.close(); // $(window.frames["iframe1105"]).load(function(){ // $("#iframe1105").height($("#iframe1105").contents().find("body").height()+showittryitheight); // }); // code1105 = newCode; // } }); $(".tryButton1105").click(function(){ $("#tryDiv1105").show(); $("#stepcodeTextarea1105").focus(); $("#stepcodeTextarea1105").height(200); $("#iframe1105").height(0); window.frames["iframe1105"].document.write("
"); window.frames["iframe1105"].document.write(decodeHtml($("textarea#stepcodeTextarea1105").val())); window.frames["iframe1105"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1105"]).load(function(){ $("#iframe1105").height($("#iframe1105").contents().find("body").height()+showittryitheight); }); $("#iframe1105").height($("#iframe1105").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1105.focus(); editor1105.setSize(null, "250"); $("#rendering1105").hide(); $("#rendered1105").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1105 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1105"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1105.on("change",function(doc){ if(!binded1105){ $(window).bind('beforeunload',function(){ binded1105 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1105 = newCode; $("textarea#stepcodeTextarea1105").val(newCode); if(alreadyWriteCode1105!=code1105){ lastModifedTime1105 = new Date().getTime(); $("#rendering1105").show(); $("#rendered1105").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1105 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1105.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1105").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JavaScript-逻辑运算符 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图