Chapter 4
Variables, Operators and Datatypes
Variables
• Variables are used to store data values.
• Variables have:
• Name
• Datatype
• Value
• JavaScript uses the keywords var and/or let to declare variables.
• An equal sign/assignment operator(=) is used to assign values to variables.
• Example:
var age;
age=15;
Note: JavaScript does not interpret LET or Let as the keyword let.
Operators
JavaScript uses:
• Arithmetic operators
• Assignment operator
• Comparison operators
• Logical operators
JavaScript Keywords
• JavaScript keywords are used to identify actions to be
performed.
• Some JavaScript keywords are:
• let
• var
JavaScript Identifiers
• Identifiers are used to name variables and keywords.
• Identifiers can be short names (like x and y) or more descriptive names
(age, lastname, customeraddress).
The rules for the naming JavaScript Variables
JavaScript name can contain:
• A letter (a – z or A – Z)
• A dollar sign ($)
• An underscore(_)
JavaScript name must begin with letter.
Note: JavaScript identifiers are all case sensitive.
Data Types
• Unlike its predecessor languages C, C++ and Java,
JavaScript does not require variables to have a declared
type before they can be used in a script.
• A variable in JavaScript can contain a value of any data
type, and in many situations JavaScript automatically
converts between values of different types for you.
Declaring a JavaScript variable
• Creating a variable in JavaScript is called “declaring” a
variable.
• var and let keywords are declaring variable.
Example:
var age; or let age;
Arithmetic operators
Addition & Subtraction
Multiplication & Division
<script> <script>
let x=5; let x=5;
let y=3; let y=3;
let z= x + y; let z= x * y;
let m= x - y; let m= x / y;
[Link](z); [Link](z);
[Link](m); [Link](m);
</script> </script>
Comparison/Relational
Operators
<script>
let x=5;
let y=3;
[Link](x>y);
[Link](x<y);
</script>
Logical operator
<script>
let x= true && true;
let y= true || false;
let z=!false;
[Link](x);
[Link](y);
[Link](z);
</script>