Last Update: Fri, 04 Feb 2022 18:31:04
• ADS
• CSS
• EXCEL
• SCRIPTS
• SEO
• WEBSITE-MONITORING-AND-BACKUP
In JavaScript, identifiers are used to name variables, keywords, functions and labels. In JavaScript, the first character must be a letter, or an underscore (_), or a dollar sign ($). Subsequent characters may be letters, digits, underscores, or dollar signs. All JavaScript identifiers are case sensitive. E.g. the variables "lastName" and "lastname" are two different variables:
var a, b, c; // Declare 3 variables a = 5; // Assign the value 5 to a b = 6; // Assign the value 6 to b c = a + b; // Assign the sum of a and b to c var person = "Hege";
5 is a number while "5" is a string. When we use + sign with numbers, it adds them and when we use with string, it concatenates (joins) them. If you put a number in quotes, the rest of the numbers will be treated as strings, and concatenated.
<!DOCTYPE html> <html> <body> <h2>JavaScript Variables</h2> <p>The result of adding "5" + 2 + 3:</p> <p id="demo"></p> <script> var x = "5" + 2 + 3; document.getElementById("demo").innerHTML = x; </script> </body> </html>Run the above code (8)
Not all JavaScript statements are "executed". Code after double slashes // or between /* and */ is treated as a comment. Comments are ignored, and will not be executed.
// single line comment
/*
Multi line comment
*/
Arithmetic operators are used to perform arithmetic on numbers:
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement
JavaScript Assignment Operators:
x += y x = x + y
x -= y x = x - y
x *= y x = x * y
x /= y x = x / y
x %= y x = x % y
x **= y x = x ** y
The + operator can also be used to add (concatenate) strings.
<!DOCTYPE html> <html> <body> <h2>JavaScript Operators</h2> <p>The assignment operator += can concatenate strings.</p> <p id="demo"></p> <script> var txt1; txt1 = "What a very "; txt1 += "nice day"; document.getElementById("demo").innerHTML = txt1; </script> </body> </html>Run the above code (9)
<!DOCTYPE html> <html> <body> <h2>JavaScript Operators</h2> <p>Adding a number and a string, returns a string.</p> <p id="demo"></p> <script> var x = 5 + 5; var y = "5" + 5; var z = "Hello" + 5; document.getElementById("demo").innerHTML = x + "<br>" + y + "<br>" + z; </script> </body> </html>Run the above code (10)