-
Notifications
You must be signed in to change notification settings - Fork 2
Javascript Cheatsheet
#Comments
Any to the right of //
in Javascript is a comment and will be ignored by the computer.
We use them to make notes and help other programmers understand what is happening.
// this is a comment
// so is this! var x = 1 never gets run
#Values There are different types of values you can use in Javascript. The main types are:
- Number (
1
,-214
,3.14159
) - String (
"hello"
,""
,"this is a string with symbols (!@)!@#}{"
) - Boolean (
true
,false
)
A number is exactly what you would expect. You can do regular math with numbers in Javascript.
1 + 1
=> 2
4 - 3
=> 1
2 * 2
=> 4
12 / 4
=> 3
// % is called modulus - it tells you the remainder after division
// for example 11/5 = 2 remainder 1
11 % 5
=> 1
A string is any text surrounded by quotes. You can perform simple operations on strings:
"a string".length
=> 8
//substring gives you a piece of a string.
//the first number is the starting position
//and the second is the ending position + 1
//remember that the positions start at 0!
//here's a couple examples:
var s = "another string";
s.substring(0, 3);
=> "ano"
s.substring(2, 3);
=> "o"
s.substring(6, 6);
=> ""
s.substring(0, s.length);
=> "another string"
//you can also add strings and other values together with +
var s = "Hello ";
var name = "Bob";
console.log(s + name + 1);
=> Hello Bob1
A boolean is value that can be either true or false. You can use the words true
and false
directly, or you can compare other values and get a boolean as the result. You can also combine booleans using the or and and operators.
1 > 3
=> false
//to compare for equality, use the '===' operator
12.3 === 12.3
=> true
//to compare for inequality, use the '!=' operator
16 != 42
=> true
"foo".length < 5
=> true
//&& means "and". If both boolean values are true, the result is true.
"foo".length < 5 && "foo" != "bad string"
=> true
"foobar".length < 5 && "foobar" != "bad string"
=> false
//|| means "or". If either boolean value is true, the result is true.
"foobar".length < 5 || "foobar" != "bad string"
=> true
"bad string".length < 5 || "bad string" != "bad string"
=> false
#Output
console.log
will print out the value or variable that you give it.
console.log(1);
=> 1
console.log("asdf");
=> asdf
console.log(false);
=> false
#Variables
##Declaring a variable
The first time you want to use a variable, you must declare it with var
and give it a name and value.
var x = 1;
var y = "baz";
var somethingelse = true;
##Using a variable Once a variable is declared, you can use it anywhere you would use a value.
var x = 1;
console.log(x);
=> 1
var x2 = x + 1;
console.log(x2);
=> 2
var y = "this is a string";
console.log(y);
=> this is a string
##Modifying a variable You can also change the value of a variable after it has been set.
var x = "foo";
console.log(x);
=> foo
x = "buh this isn't foo anymore";
console.log(x);
=> buh this isn't foo anymore
#If Statements An if statement lets you run different code depending on the condition. The condition must be any value or expression which results in a boolean value.
if (true) {
console.log("the condition is true");
} else {
console.log("the condition is false");
}
=> the condition is true
//you can use any comparison for the condition
var x = 12;
if (x < 6) {
console.log("the condition is true");
} else {
console.log("the condition is false");
}
=> the condition is false
//you can also only have an if case
var y = "foobar";
if (y.length < 12) {
console.log("the condition is true");
}
=> the condition is true
var y = "foobar";
if (y.length > 12) {
console.log("the condition is true");
}
=> <Nothing happens>
TBD