JAVASCRIPT
VARIABLES
New variables in JavaScript are declared using one of three keywords: let, const, or var.
let allows you to declare block-level variables. The declared variable is available from the block it
is enclosed in.
let a;
let name = 'Simon';
The following is an example of scope with a variable declared with let:
// myLetVariable is *not* visible out here
for (let myLetVariable = 0; myLetVariable < 5; myLetVariable++) {
// myLetVariable is only visible in here
}
// myLetVariable is *not* visible out here
const allows you to declare variables whose values are never intended to change. The variable is
available from the block it is declared in.
const Pi = 3.14; // variable Pi is set
Pi = 1; // will throw an error because you cannot change a constant variable.
varis the most common declarative keyword. It does not have the restrictions that the other two
keywords have. This is because it was traditionally the only way to declare a variable in
JavaScript. A variable declared with the var keyword is available from the function it is declared in.
var a;
var name = 'Simon';
An example of scope with a variable declared with var:
If you declare a variable without assigning any value to it, its type is undefined.
An important difference between JavaScript and other languages like Java is that in JavaScript,
blocks do not have scope; only functions have a scope. So if a variable is defined using var in a
compound statement (for example inside an if control structure), it will be visible to the entire
function. However, starting with ECMAScript 2015, let and const declarations allow you to create
block-scoped variables.
OPERATORS
JavaScript's numeric operators are +, -, *, / and % which is the remainder operator (which is the
same as modulo.) Values are assigned using =, and there are also compound assignment
statements such as += and -=. These extend out to x = x operator y.
x += 5;
x = x + 5;
You can use ++ and -- to increment and decrement respectively. These can be used as a prefix or
postfix operators.
The + operator also does string concatenation:
'hello' + ' world'; // "hello world"
If you add a string to a number (or other value) everything is converted into a string first. This
might trip you up:
'3' + 4 + 5; // "345"
3 + 4 + '5'; // "75"
Adding an empty string to something is a useful way of converting it to a string itself.
Comparisons in JavaScript can be made using <, >, <= and >=. These work for both strings and
numbers. Equality is a little less straightforward. The double-equals operator performs type
coercion if you give it different types, with sometimes interesting results:
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller Handelswetenschapperugent. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $3.21. You're not tied to anything after your purchase.