JavaScript is a programming language that has become in use for Front and Back end applications. It brings a page to “life” so to speak. It makes your page dynamic and interactive with your user. The difference between JavaScript or JS for short compared to HTML and CSS is that HTML & CSS are only markup languages and cannot perform any functions where in JS you use logic to make the page do what you need it to do.
You start by linking your JS page to your HTML similiar to the way you would for CSS but instead of using the <link> tag you would use the <script> tag. Normally you would like to put this <script> right above the ending </body> tag to help ensure your HTML and CSS code loads up on your page before the JavaScript does-this will eliminate headaches and problems in the future. It will look something like <script src=”yourfilename.js>.
There are different types of values you can use in JavaScript- Number, Boolean, String, undefined, and Null. For now we will focus on the first 3 since in my opinion they are more common.
Number values are exactly what you would think they are numbers. Whole numbers are integers and fractional numbers with decimals are referred to as floats. These number values can be used to perform arithmetic using mathematical operators such as addition, subtraction, multiplication and division-but the one where you rarely will learn in highschool is the modulo which looks like a percentage sign % but it actually performs division but it only returns the remainder of the operation. This might sound impracticable but is very useful especially when you want to find if a number is even or odd. If you “mod” a number by 2 and it returns a 0 that indicates the number is even, if it returns a 1 then your number is odd – 9 % 2 returns a 1 because its 4 remainder 1. Speaking of highschool, remember (“Please Excuse My Dear Aunt Sally”) or PEMDAS -parenthesis, exponents, multiplication, division, addition, subtraction- The order in which mathematical operations are executed matters. Whatever expression is in the parenthesis will be executed first then exponents, multiplication etc. So keep this in mind while you are writing logic in js.
Boolean Values have only two possible values, True or False. Boolean value is can be thought of as a polygraph test-it is testing to see the truth in something.
A string value is a collection of characters wrapped in quotation marks. You can use either single quotes ‘ ‘ or double quotes ” ” to encapsulate your characters. You must stick with either or when you are wrapping up your characters-if you start with single quotes you must end with them as well. For example “this is valid” but “this is not valid’ or ‘this” . If you would like to write a sentence that contains quotes in it; you would need to use different style quotes for the entire string and for the characters that need quotes. For example ‘this is a string “using different style quotes” within the string’. You can add line breaks to string values using the /n character; the “n” after the backlash tells the computer you want to start a new line. This is known as escaping the character. You can also use the + operator and in strings case it will combine multiple string values into one string value. This is known as concatenation.
Other operators besides the mathematical operators are comparison operators and logical operators. Comparison operators are used to compare two values and return a result of true or false. Less than (<), Greater than (>), Less than or equal to (<=), Greater than or equal to (>=), Equal to (==), Strictly equal to (===), Not equal to (!=), Not strictly equal to (!==+). The first five should be obvious. The (===) was added later to JS to fix some confusion with (==). There are some instances where (==) will return true when it is false and vice versa. The (!=) or not equal and the (!==) not strictly equal are used for checking if a value is not equal to another value. All comparisons produce Boolean values.
Logical operators are three- And (&&), or (||) , and Not( !). && is a binary operator which means it will operate on two operands or values to a return a result, so it will take two Boolean values (true or false) and will return a “true” only if both values are true.
true && true results a TRUE
true && false returns a FALSE
The Or operator or (||)- called pipes-will produce a true value if either of the values are true.
true || false returns a TRUE
false || false returns a FALSE
The Not operator (!) will flip a Boolean value that is true to false and vice versa.
!true = FALSE
!false= TRUE