var name ='';if(name ==='zhangsan'){alert('这是张三');}else{
console.log('这不是张三')}
if …elseif…else语句
<label for="weather">Select the weather type today:</label><select id="weather"><option value="">--Make a choice--</option><option value="sunny">Sunny</option><option value="rainy">Rainy</option><option value="snowing">Snowing</option><option value="overcast">Overcast</option></select><p></p>var select = document.querySelector('select');var para = document.querySelector('p');
select.addEventListener('change', setWeather);functionsetWeather(){var choice = select.value;if(choice ==='sunny'){
para.textContent ='It is nice and sunny outside today. Wear shorts! Go to the beach, or the park, and get an ice cream.';}elseif(choice ==='rainy'){
para.textContent ='Rain is falling outside; take a rain coat and a brolly, and don\'t stay out for too long.';}elseif(choice ==='snowing'){
para.textContent ='The snow is coming down — it is freezing! Best to stay in with a cup of hot chocolate, or go build a snowman.';}elseif(choice ==='overcast'){
para.textContent ='It isn\'t raining, but the sky is grey and gloomy; it could turn any minute, so take a rain coat just in case.';}else{
para.textContent ='';}}
switch语句
<label for="weather">Select the weather type today:</label><select id="weather"><option value="">--Make a choice--</option><option value="sunny">Sunny</option><option value="rainy">Rainy</option><option value="snowing">Snowing</option><option value="overcast">Overcast</option></select><p></p>var select = document.querySelector('select');var para = document.querySelector('p');
select.addEventListener('change', setWeather);functionsetWeather(){var choice = select.value;switch(choice){case'sunny':
para.textContent ='It is nice and sunny outside today. Wear shorts! Go to the beach, or the park, and get an ice cream.';break;case'rainy':
para.textContent ='Rain is falling outside; take a rain coat and a brolly, and don\'t stay out for too long.';break;case'snowing':
para.textContent ='The snow is coming down — it is freezing! Best to stay in with a cup of hot chocolate, or go build a snowman.';break;case'overcast':
para.textContent ='It isn\'t raining, but the sky is grey and gloomy; it could turn any minute, so take a rain coat just in case.';break;default:
para.textContent ='';}}
三元运算符
var greeting =(isBirthday)?'Happy birthday Mrs.Xiao':'Today isnot my birthday';--------------------------<label for="theme">Select theme:</label><select id="theme"><option value="white">White</option><option value="black">Black</option></select><h1>This is my website</h1>var select = document.querySelector('select');var html = document.querySelector('html');
document.body.style.padding ='10px';functionupdate(bgColor, textColor){
html.style.backgroundColor = bgColor;
html.style.color = textColor;}
select.onchange=function(){( select.value ==='black')?update('black','white'):update('white','black');}
if …else语句var name = '';if(name === 'zhangsan'){ alert('这是张三');}else{ console.log('这不是张三')}if …elseif…else语句<label for="weather">Select the weather type today: </label><select id="weather"> <option value="">--Make a choi