Download presentation
Presentation is loading. Please wait.
Published byVincent Jefferson Modified over 9 years ago
2
JavaScript Variables Like other programming languages, JavaScript variables are a container that contains a value - a value that can be changed/fixed as required.
3
Declaring JavaScript variables First, you need to declare your variables. You do this using the var keyword. You can declare one variable at a time or more than one. You can also assign values to the variables at the time you declare them.
4
Declaring JavaScript variables // declaring one JavaScript variable var firstName; // declaring multiple JavaScript variables var firstName, lastName; // declaring and assigning one JavaScript variable var firstName = 'Homer'; // declaring and assigning multiple JavaScript variables var firstName = 'Homer', lastName = 'Simpson';
5
Using JavaScript variables Although there are many uses for JavaScript variables, here is a quick and simple example: var firstName = prompt("your first name?", ""); document.write(firstName);
6
Rules for JavaScript Variables 1.JavaScript variables' names can contain any letter of the alphabet, digits 0-9, and the underscore character. 2.No spaces 3.No punctuation characters (e.g comma, full stop, etc) 4.The first character of a variable name cannot be a digit. 5.JavaScript variables' names are case-sensitive. For example: firstName and FirstName are two different variables.
7
Example Variables and Constants Variables and Constants var amountDue; amountDue = 0; var productPrice = 5; var quantity = 2; amountDue = productPrice * quantity; alert ( amountDue );
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.