Variables are usually representative names present in the program. Take the example like X may certainly stand for any unknown value in algebra, variables which are used in programming for representing something else. Variables are like containers which usually contain data. You can give names to containers and later change the data in a variable right by using the name.
Without variables, each and every computer program certainly should have one purpose. In any one-line program, there is no use of variables:
alert(3 + 7);
The purpose is certainly to add together the numbers 3 and 7 and then to print the result in the popup window of a browser. There is not much use of the program unless one needs to recall the sum of 3 and 7 on daily basis. With variables, there can be a general purpose program which can add any two numbers together and then print out the result like:
var firstNumber = 3; var secondNumber = 7; var total = Number(firstNumber) + Number(secondNumber); alert (total);
The program can also be modified by expanding the program asking the user for two numbers and then adding them together like:
var firstNumber = prompt("Enter the first number"); var secondNumber = prompt("Enter the second number"); var total = Number(firstNumber) + Number(secondNumber); alert (total);
The Document Will Read As Follows:
<html> <head></head> <body> <script> var firstNumber = prompt("Enter the first number"); var secondNumber = prompt("Enter the second number"); var total = Number(firstNumber) + Number(secondNumber); alert (total); </script> </body> </html>
How To Declare A Variable
- With the use of var keyword: var myName; a variable created with var keyword will surely have an initial value of undefined unless one give a value when created such as varmyName= “ Tara”;
- Without the use of var keyword: myName = “Tara”; When a variable is created without a var keyword then it becomes a global variable.
Idea About The Global And Local Scope
- Global variables which can be used anywhere inside the program
- Local (function) variable are again variables which can be created right inside of protected program well within the program termed as function.
Local variables are usually preferable to using globals as limiting scope of the variables will reduce the chance of accidentally overwriting the value of a variable with another variable of the same name. Using globals can create problems in the program making it difficult to track it down and fix. It is advisable never to create variables without the use of var keyword. Global variable can also be created with the use of a var keyword and can be done in that way.
Creating Constants With The Use Of Const Keyword
In some occasion, the program may have need for variables which cannot be changed. In those cases, one can declare the variable with the use of const keyword. For example:
const heightOfTheEmpireStateBuilding = 1454; const speedOfLight = 299792458; const numberOfProblems = 99; const meanNumberofBooksReadIn2014 = 12;
In most cases, constants usually abide the same rules like other variables but once one can create a constant then its value cannot be changed right during the lifetime which will last during lifetime.
Working With Data Types
Number Data Type
- Numbers which are formatted as text strings are usually converted right to numbers which can be used for calculations like:
Number("42") // returns the number 42
parseFloat("10"); // returns 10 parseFloat(100.00); //returns 100.00 parseFloat("10"); //returns 10
- charAt () will produce the character right at a specified position. Always remember that counting the characters usually start with (0).
- concat () will combine one or more strings and then return the incorporated string.
- indexOf () will search and then return the position right at the first occurrence of the right searched character or substring right within the string.
- split() will splits strings into various array of substrings:
- substr() will extract a portion of the string right from the “start” through a specified length:
- substring() will extract characters within a string between two particular specified positions:
- toLowerCase() will give you string with all the characters converted to lowercase:
- toUpperCase() will give you the string with all of the characters which are converted to uppercase:
Boolean Data Type
Boolean data type or Boolean variables store one of the two possible values which can be either true or false. This term Boolean is named after George Boole (1815- 1864) creating an algebraic system of logic. Since it is the name of the person, so it is written with initial capital letter.
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> var isItGreater = Boolean (20 > 2); document.getElementById("demo").innerHTML = isItGreater; </script> </body> </html>
NaN data type usually stands for Not a Number. It is usually the result one can get when they try to do math with a string or when calculation fails or it cannot be done. Take the example like it is impossible to calculate the square root of a negative number.
<script> var areTheySame = Boolean ("tiger" === "tiger"); document.getElementById("demo").innerHTML = areTheySame; </script>
- Best React Performance Optimization Tips
- The Pros And Cons Of Native vs Hybrid Mobile App Development
- Top Swift Design Patterns For iOS App in 2022
- Deep Learning: Practices, Applications and Future
- Deciphering Intricate Declarations Of C: The Concept Of C Declarators
- A Comprehensive Guide To Arrays In PHP | Everything You Need To Know
- Android courses
- App Development
- Artificial Intelligence Course
- Cloud Computing Course
- Content Writing
- Crash Course
- Digital Marketing
- Embedded System and Robotics Course
- Ethical Hacking and Cyber Security Course
- How To
- Internet of Things (IoT) Course
- iOS App Development
- iPhone courses
- IT Certification Course
- Machine Learning with Python Course
- Mean stack
- Mern stack
- NET Development
- Networking with Cisco Tools Course
- PHP Variable
- Professional IT course
- Python Course
- React Native
- SAP Development
- Social Media
- Software Testing
- Successful Career
- Tech Industry
- Tech News & Tips
- UI/UX Design Course
- Web Design