Just like in C#, JavaScript utilizes conditional, control-flow statements. One of the most common is the IF statement. IF statements evaluate Boolean statements…greater than, less than, etc. Equal/Not equal is another evaluation statement. In JavaScript, there are two ways to work with equal/not equal. You can utilize two or three equal signs. Two equal signs evaluates value only. Three equal signs evaluates value and type. For example…
if( 2 == ‘2’ ){
console.log( ‘If returns true‘ ); // This should run due to using two equal signs.
}
if( 2 === ‘2’ ){
console.log( ‘If returns true‘ ); // This should NOT run due to using three equal signs and types being different.
}
The same principle applies to not equal…
if( 2 != ‘3’ ){
console.log( ‘If returns true‘ ); // This should run due to using two equal signs.
}
if( 2 !== ‘3’ ){
console.log( ‘If returns true‘ ); // This should NOT run due to using three equal signs and types being different.
}
Other than the aforementioned JavaScript specific aspects, the same control-flow statements exist in JavaScript as in C#…
- if/else
- switch
- for loop
- while loop
Happy Coding!
Clay Hess