JavaScript 2022

Term 3 Wednesday

May - July 2022

This site will be updated live during the sessions

Week 2 Examples 1

		
001let age = 53;
		
002
		
003//age = "fifty three";
		
004
		
005
		
006console.log(age++); // 53
		
007
		
008console.log(age); //54
		
009
		
010age--;
		
011console.log(age); //53
		
012
		
013// if statement
		
014
		
015if(age > 18)
		
016{
		
017	console.log("You can buy a drink in the pub");
		
018}
		
019else if(age>=5)
		
020{
		
021	console.log("You can ask your parents to buy you a soft drink");
		
022}
		
023else
		
024{
		
025	console.log("Sorry, you can't come in!");
		
026}
		
027
		
028
		
029// order is important!!!
		
030// age of 53
		
031
		
032if(age>=5)//true
		
033{
		
034	console.log("You can ask your parents to buy you a soft drink");
		
035}
		
036else if(age > 18)
		
037{
		
038	console.log("You can buy a drink in the pub");
		
039}
		
040else
		
041{
		
042	console.log("Sorry, you can't come in!");
		
043}
		
044
		
045// equals operator - looks at value only
		
046
		
047let string = "20"; //data type is string
		
048let number = 20;  // data type is number
		
049
		
050console.log(string==number); //true
		
051
		
052// identical operator looks at both data type and value
		
053console.log(string===number); //true
		
054
		
055let string2 =''; // evaluate as false if using ==
		
056let number2 = 0; // evaluate as false if using ==
		
057
		
058// if statement
		
059
		
060if(age > 18 && age < 21)
		
061{
		
062    console.log("There is a free drink for you at the bar!");
		
063}
		
064else if(age > 18)
		
065{	
		
066	console.log("You can buy a drink in the pub");
		
067}
		
068else if(age>=5)
		
069{
		
070	console.log("You can ask your parents to buy you a soft drink");
		
071}
		
072else
		
073{
		
074	console.log("Sorry, you can't come in!");
		
075}
		
076///using the ! operand
		
077let overdrawn = false;
		
078if(!overdrawn)//same as overdrawn == false, which is true!
		
079{
		
080	console.log("You can withdraw cash");
		
081}
		
082
		
083// ternary 
		
084let outputString = ''
		
085outputString = (overdrawn===false)?  "you can withdraw" : "No cash for you";
		
086console.log(outputString);
		
087