Hi dear welcome to "CodePrime" Blog . In this post you are going to learn How to use If, Else, and Else-if conditional statement in javascript. First Creat A index.html file Copy This HTML CODE and pest it one your code editor. 2ndly creat if-else-if.js file Then Copy The JS CODE and pest it. Next connect if-else-if.js file with your index.html then open the index.html file with your browser.
HTML CODE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="/js/if-else-if.js"></script>
<title>javascript</title>
</head>
<body>
<h2>Javascript Tutorial</h2>
<h3>By Codeprime</h3>
</body>
</html>
JAVASCRIPT CODE
let numbers=[];
numbers=[1,2,1,2,3,1,2];
if(numbers[0]==numbers[2]) {
console.log('its correct');
};
// if else example
if(numbers[0]==numbers[3]) {
console.log('its correct');
} else{
console.log(' its wrong');
}
//else-if example
if(numbers[0]==numbers[3]) {
console.log('1st block');
} else if (numbers[1]==numbers[3]) {
console.log(' 2nd block');
} else {
console.log(' 3rd block');
}
0 Comments