- JavaScript Introduction
- JavaScript Where To
- JavaScript Output
- JavaScript Statements
- JavaScript Syntax
- JavaScript Comments
- JavaScript Variables
- JavaScript Let
- JavaScript Const
- JavaScript Operators
- JavaScript Arithmetic
- JavaScript Assignment
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Const
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Random
- JavaScript Booleans
- JavaScript Comparison And Logical Operators
- JavaScript If Else And Else If
- JavaScript Switch Statement
- JavaScript For Loop
- JavaScript Break And Continue
- JavaScript Type Conversion
- JavaScript Bitwise Operations
- JavaScript Regular Expressions
- JavaScript Errors
- JavaScript Scope
- JavaScript Hoisting
- JavaScript Use Strict
- The JavaScript This Keyword
- JavaScript Arrow Function
- JavaScript Classes
- JavaScript JSON
- JavaScript Debugging
- JavaScript Style Guide
- JavaScript Best Practices
- JavaScript Common Mistakes
- JavaScript Performance
- JavaScript Reserved Words
- JavaScript Versions
- JavaScript History
- JavaScript Forms
- JavaScript Validation API
- JavaScript Objects
- JavaScript Object Properties
- JavaScript Function Definitions
- JavaScript Function Parameters
- JavaScript Function Invocation
- JavaScript Closures
- JavaScript Classes
- Java Script Async
- JavaScript HTML DOM
- The Browser Object Model
- JS Ajax
- JavaScript JSON
- JavaScript Web APIs
- JS Vs JQuery
JavaScript Variables
Re-Declaring JavaScript Variables
If you re-declare a JavaScript variable, it will not lose its value.
The variable carName will retain the value "Volvo" after executing the following statements:
var carName;
JavaScript Arithmetic
As with algebra, you can do arithmetic with JavaScript variables, using operators like =
and +
:
JavaScript Dollar Sign $
Remember that identifiers (names) in JavaScript must begin with:
A letter (A-Z or a-z)
A dollar sign ($) Or an underscore (_)
Since JavaScript treats the dollar sign as a letter, variable names containing $ are valid.
var $ = 2;
var $myMoney = 5;
Professional programmers frequently use the dollar sign as an alias for the primary function in a JavaScript library, although its use is uncommon in JavaScript.
In the jQuery JavaScript library, for example, the $ function is used to select HTML elements. In jQuery $("p"); means "select all p elements".
JavaScript Underscore (_)
Since JavaScript treats underscore as a letter, identifiers containing _ are valid variable names:
var _x = 2;
var _100 = 5;
Using the underscore in JavaScript is uncommon, but it is customary for professional programmers to use it as an alias for "private" variables.
JavaScript Variables
JavaScript variables are containers for storing data values.
x, y, and z are variables declared with the var keyword in this example.
var y = 6;
var z = x + y;
x holds the value 5 while y holds the value 6
11 is stored in the value z
Using Let And Const (ES6)
Before 2015, the only way to declare a JavaScript variable was with the var keyword.
The 2015 version of JavaScript (ES6) permits the use of the const and let keywords to define variables that cannot be reassigned and variables with restricted scope, respectively.
Due to the complexity of describing the distinction between these keywords and the fact that they are not supported in older browsers, the first section of this tutorial will predominantly use var.
Safari 10 and Edge 14 were the first web browsers to support ES6 in its entirety:
Chrome 58 | Edge 14 | Firefox 54 | Safari 10 | Opera 55 |
Jan 2017 | Aug 2016 | Mar 2017 | Jul 2016 | Aug 2018 |
Much Like Algebra
In this example, the variables are price1, price2, and total:
var price2 = 6;
var total = price1 + price2;
???????In programming, just like in algebra, we use variables (like price1) to hold values.
Similar to algebra, variables are used in programming expressions (total = price1 + price2)
Based on the preceding example, the total can be calculated to be 11.
JavaScript variables are containers for storing data values.
JavaScript Identifiers
All JavaScript variables must have distinctive names.
These distinct names are known as identifiers.
Identifiers can be either abbreviated names (such as x and y) or more descriptive names (age, sum, totalVolume).
The general guidelines for constructing variable names (unique identifiers) are as follows:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter
- Names can also begin with $ and _ (but we will not use it in this tutorial)
- Names are case sensitive (y and Y are different variables)
- Reserved words (like JavaScript keywords) cannot be used as names
JavaScript identifiers are case-sensitive.
The Assignment Operator
The equal sign (=) in JavaScript is a "assignment" operator, not a "equal to" operator.
This is dissimilar to algebra. The following does not make sense in algebra:
(It computes x plus 5 and inserts the result into x. x has its value increased by 5.)
In JavaScript, the "equal to" operator is written as ==.
JavaScript Data Types
JavaScript variables can store both numeric and textual values, such as 100 and "John Doe."
Text values in programming are referred to as text strings.
JavaScript can manage a variety of data types, but for now, consider only numbers and strings.
Strings are enclosed in double or single quotation marks. They are written without quotation marks.
When a number is enclosed in quotation marks, it is treated as a text string.
var person = "John Doe";
var answer = 'Yes I am!';
Declaring (Creating) JavaScript Variables
Creating a variable in JavaScript is called "declaring" a variable.
You declare a JavaScript variable with the var
keyword:
var carName;
After the declaration, the variable has no value (technically it has the value of undefined
).
To assign a value to the variable, use the equal sign:
carName = "Volvo";
You can also assign a value to the variable when you declare it:
var carName = "Volvo";
In the example provided below, the variable carName is created and assigned the value "Volvo."
The value is then "output" within an HTML paragraph with the id="demo" attribute:
<script>
var carName = "Volvo";
document.getElementById("demo").innerHTML = carName;
</script>
One Statement, Many Variables
You can declare many variables in one statement.
Commence the statement with var and separate each variable with a comma:
???????
carName = "Volvo",
price = 200;
Value = Undefined
Variables are frequently declared in computer programmes without a value. The value may be something that must be computed or something that will be supplied later, such as user input.
The value of a variable declared without a value is undefined.
The value of the variable carName will be undefined following the execution of this statement:
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP