Not going to speechify anymore, straight to the point right now…
Data Types and Operators
console.log("Here is a string, which is wrapped in a double quote!"); console.log("Here is also a string, which is inside a single quote!"); console.log(`Likely, this text still is a string, with a little different!`); (ES6: not worry about it now)
Choosing ” ” (double quote), ‘ ‘ (single quote), or ` ` it depends on your style, but in some
As you can see, when you use double quote to create a string, you should use single quote inside it and vice versa to avoid an error:
Another way more optimal to contain a quote in a string is using backslash \ :
No more error, with whatever string styles you want as long as using
Two or more strings to another can be concatenated by using
console.log("Hello," + " my name is " + "Scary Terry!"); // Hello, my name is Scary Terry!
Bracket notation and the immutable of a string
Bracket notation is a way to get a character at a specific
indexwithin a string.
To have you can envision it easily, we have an example like this:
"Snoopy" // the zero index is "S"
As you can see the zero
Snoopy and assigning it to a variable
var firstName = "Snoopy" // declare a variable, scroll down to see the description about it. firstName // "S"
Get the second character (“n”) by
String values are immutable, which means that they cannot be altered once created. For example:
var greeting = "Hello"; greeting = "J"; // cannot change to "Jallo"; greeting; // still "Hello"
But you still can change the string’s value by assign it to a new value:
Operators and Boolean Values
- The addition operation ( + ) is used to plus/add numbers, it’s also can concatenate a string:
console.log(2 + 3); // 5 console.log("The lazy" + " dog"); // The lazy dog
- The subtraction operator ( – ) is used to subtract numbers, but not being used to for strings:
console.log(6 - 4); // 2 console.log("Hey" - "boyssss") // NaN
- The multiplication operator (
*) multiplies the given numbers:
console.log(6 * 9); // 54
- The division operator ( / ) is used to divide the given numbers:
console.log(27 / 3); // 9
- The modulus operator (or division remainder) ( % ) is operated to divide numbers and giving the remainder of the calculation:
console.log(54 % 7); // 5; console.log(128 % 8); // 0;
- The exponentiation operator (**) is used to calculate the
exponentpower, that is,
console.log(2**3); // 8 console.log(11**2); // 121
- The increment operator (++) is used to increase a number by one. But there are two ways to manipulate this operator. For example, we have two things call “x” which we will use increment operator:
++x (prefix) which means x will return first, then increase by one after. x++ (postfix) which is increased by one to its value first, then return the result. (postfix): x = 3; a = x++; // a = 3; x = 4 (return first, then increase.) (prefix): x = 3; a = ++x // a = 4; x = 4 (increase first, then return the value.)
- Similar to the increment operator, the decrement operator (–) works, but instead of adding number by one, it subtracts it by one:
(postfix): x = 2; a = x--; a = 2; x = 1 (prefix): x = 2; a = --x; a = 1; x = 1
Boolean Values and Logical Operators
Boolean is a statement with a certain condition, either return true which is something is true or false which is something is false. (just want to lengthen my words, don’t think about it.)
>which means greater than,
<which means less than something:
console.log(69 > 96); // false console.log(20 > 15.5); // true console.log(52 < 58); // true
aregreater than or equal to and less than equal to something, respectively.
console.log(20 >= 15); //true console.log(10 <= 11); //true
- Other similar operators are == (equal to) and != (not equal to): you can compare strings, numbers, arrays, objects, etc…(don’t worry about arrays or objects now.) Note:
!(an exclamation mark) is an operator that flips the value, !true is false, !false is true. If true and false are compared with a number, true should equal to 1 and 0 should false.
console.log("Donald Trump" != "Bernie Sanders"); // true console.log(20 == 21); // false console.log(13 != 59); // true console.log(true == 1); // true console.log(false == 0); // true
inthe bare word we’d say “and”. It will only return true in case both values it’s given are true. Otherwise, false.
console.log(true && true); // true console.log(true && false); // false console.log( 23 + 4 * 6 == 382 && 23 > 2 + 4) // false (you can also mix boolean with arithmetic.
||is also a logical operator, called or. Not like the && operator that both values must be true,
||operator just needs one.
console.log(false || false); // false (true not appears here) console.log(true || false); // true
Remember examples of increment and decrement above? Actually, we’ve been created a variable and its name “x” and “a”. In order to create a variable and assign it to a value, the value could be a string, a number, an array, an object or function, etc…(don’t worry about those things now) simply you just pick define a variable’s name that you like (but should be transparent), just simple like this, but I just want to show you a way to declare variable, you shouldn’t try to create a variable by this way:
x = 2 + 3 - 5; console.log(x); // 0 a = "This is a"; b = " great tutorial, maybe?"; console.log(a + b); // This is a great tutorial, maybe? c = 2*3; d = 8 - 4; sum = c + d; console.log(sum); // 10
It’s much better if you declare a variable with
- Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
But don’t worry about the local variable and global variable now, we will talk more about it in the next few articles. Now see
var someThingSpecial = "A special string"; var anotherSpecialThing = ", really"?; var exceptionalVariable = someThingSpecial + anotherSpecialThing; console.log(exceptionalVariable) // A special string, really?
As can be observed, the fundamental developer rules
Another important thing about 2 ways define a variable above you should notice is, it not just can be assigned, it also can be re-declare, which is:
Re-assign: var x = 3; x = 4; console.log(x); // 4; Re-declare: var a = "Whoo-weeeeee!!!"; var a = "Woofffffffff!!!"; console.log(a); // Wooffffffff!
You can also define a variable with some other keywords, which are
const, but we will talk about it in the ES6 section.