Basic syntactic constructs. Emphasis on fundamental concepts & expression evaluation.
3 in your code in order to perform this operation.true
false
// Can be only this two values
null // lack of real value, "nothing"
undefined // lack real of value, "nothing"
= ) - takes value on its right side and puts it in its left-side operand (variable, object property, array index)/\* Literals \*/
// Literal of any type is an expression that resolves to itself
5 // literal
"foo" // literal
/\* Operators with operands \*/
5 + (7 * 13) // math expression
true && (false || !false) // logical expression
+"562" // unary operator
/\* Preparation \*/
let myVarA = 3
const myVarB = 2
const arr = [5, 6, 7]
function squared(number) {
return number * number
}
/\* Example \*/
55 / (12 - arr[2]) - +(true && !false) / squared(myVarB) - ++myVarA
// 55 / (12 - **7**) - +(true && !false) / squared(myVarB) - ++myVarA
// 55 / **5** - +(true && !false) / squared(myVarB) - ++myVarA
// 55 / 5 - +(true && **true**) / squared(myVarB) - ++myVarA
// 55 / 5 - +**true** / squared(myVarB) - ++myVarA
// 55 / 5 - +true / **4** - ++myVarA
// 55 / 5 - **1** / 4 - ++myVarA
// 55 / 5 - 1 / 4 - **4**
// **11** - 1 / 4 - 4
// 11 - **0.25** - 4
// **10.75** - 4
// **6.75**
[1, 2, 3] // literal
[1, "foo", true, null, undefined] // can hold values of multiple types
[[1, 2], [3, 4]] // can hold other arrays as elements
[{amount: 6}, {amount: 16}] // can hold objects as elements
const arr = [11, 22, 33]
arr[1] // get array element value, resolves to 22
arr[1] = 0 // set a element value, now arr is [11, 0, 33], overwrites existing value
arr.length // special property, it contains number of elements (is this case 3)
let counterA = 0
while (counterA < 10) {
// at the start of every iteration checks if condition is true
// If true, runs body, then checks again
// If false, then the loop is finished
console.log('counterA equals ' + counterA)
counterA++
}
for (let i = 0; i < 10; i++) {
console.log('i equals ' + i)
}
// let i = 0 - runs one time when loop starts
// i < 10 - checks condition at the start of every iteration,
// same logic as in while loop
// i++ - runs at the end of every iteration
// two loops above are functionally identical,
// but for loop encapsulates counter (i) declaration within its syntax
// and separates main action of the loop in its body
// from changing counter value
const arr = ['a', 'b', 'c']
for (let i = 0; i < arr.length; i++) {
console.log('element of arr at index ' + i + ' equals ' + arr[i])
}
let counterB = 0
while (counterB < 10) {
if (counterB === 3) {
counterB++
continue // forces immediate exit from current iteration
// goes to next iteration
// can be used in 'for' loops as well
}
console.log('counterB equals ' + counterB)
counterB++
}
let counterC = 0
while (true) { // condition will never be false
console.log('counterC equals ' + counterC)
counterC++
if (counterC === 5) {
break // forces immediate loop termination
// program execution goes further
// can be used in 'for' loops as well
}
}
{} ) - a special statement that contains other statements within it. Rarely used on its own, usually it is used as a part of another statement ('if', 'while', 'for', 'function' etc.). When used as a part of another statement it's called "body" (e.g. function body).; ).let a; let b = 5; let c; // last semicolon is not mandatory5 + 10 // two values are used to create new valuelet myVar = 7myVar = myVar + 4 // existing myVar value is used (alognside 4) to create new value// That new value overwrites existing myVar value12 // integer
3.45 // float
-512 // negative
0
Infinity // also *-Infinity*
NaN // "not a number", special value
// results from illegal operations such as *5 / "foo"*
'foo'
"foo" // same as single quotes
/\* Escaping \*/
'I don\'t know' // *I don't know*
"Jack \"Owl\" Smith" // *Jack "Owl" Smith*
"Don't need to escape"
"Use this to escape \\ in strings" // *Use this to escape \ in strings*
/\* Special characters \*/
"First line.\nSecond line"
/\*
First line.
Second line
\*/
/\* Declare & assign (initialize) \*/
var myVarA = 2 // outdated keyword, don't use
let myVarB = 4
const myVarC = 8 // can't be reassigned later
// recommended to be used by default
/\* Only declare \*/
let myVarD // has *undefined* as value
/\* Reassign \*/
myVarB = "foo"
myVarD = [1, 2, 3]
/\* Variables \*/
const myVar = "Hello world"
myVar // resolves to a value stored in the variable
/\* Object properties \*/
const human = {
name: "John",
age: 20,
}
human.age // resolves to a value stored in the property
/\* Array elements \*/
const arr = [10, 20, 30]
arr[1] // resolves to a value stored at that index
/\* Function calls \*/
// Resolves to whatever is returned by a funtion
function sum(a, b) {
return a + b
}
sum(5, 15)
const value = "I'm just a value"
// As a part of a larger expression
value + ". Or am I?"
// Assigned to variables
const newValue = value
// Assigned to object properties
const obj = {}
obj.message = value
// Put in an array at a certain index
const arr = [1, 2, 3]
arr[0] = value
// As function parameters
function isBoolean(toTest) {
return toTest === false || toTest === true
}
isBoolean(value)
// As a function returned value
function getTen() {
const value = 10
return value
}
{
key: "value",
key2: 5,
} // literal
// can hold arrays & other objects
const post = {
text: "Come and join me!",
categories: ["fun", "user-friendly", "paywalled"],
isVisible: true,
createdAt: "2022-06-12T18:58:13.059Z",
engagement: {
likes: 5,
comments: 0,
shares: 0,
},
}
post.isVisible // get value stored in property "isVisible"
post.text = "Best time of your life" // set value for property "text",
// overwrites existing value
post.engagements.like // get and set deeply nested properties
// by chaining property names
post.foo // getting non-existing property evaluates to *undefined*,
// but doesn't result in explicit error
post.foo.bar // this results in an error,
// because it's impossible to access property of *undefined*
let conditionA = true
if (conditionA) {
console.log("I will run")
}
conditionA = false
if (conditionA) {
console.log("I won't run")
}
// any condition must resolve to a boolean value.
// If an expression in condtion isn't boolean, it's converted to boolean
conditionA = 5
if (conditionA) {
console.log("I, too, will run")
}
// logical operators can be used to create complex conditions
const conditionB = false
if (conditionA && !conditionB) {
console.log("Complex condition is true!")
}
// use esle keyword to do something if condition is false
if (conditionB) {
console.log("Either I will run")
} else {
console.log("Or me")
}
// use else if keyword to check for multiple cases.
// It checks conditions until the first true condition is met,
// that branch runs, following branches are ignored
const age = 15
if (age < 5) {
console.log("Baby")
} else if (age < 16) {
console.log("Child") // this will run
} else if (age < 30) {
console.log("Young adult") // this and following branches won't run
// even though condition is true,
// because previous branch was executed already
} else if (age < 60) {
console.log("Adult")
} else { // this is optional, to run if all above condtions are false
console.log("Old man")
}
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.