Detailed Summary of JavaScript Value Types and some Operators


In computer programming, values are simply bits separated into smaller chunks that represent pieces of information. These values play different roles and every value has a type. JavaScript has about six types of values which includes Numbers, Strings, Booleans, Objects, Functions, Undefined values.
NUMBERS
Numbers are just numeric values, in JavaScript we write a number like this 
10 
3 
13
When any number is written it causes the bit pattern of that number to come into existence.
There are few things we can do with numbers and one of them is to perform arithmetical operations using some JavaScript arithmetic operators such as division, multiplication, addition and subtraction (which are /, *, +, -, respectively) these operators take two number type values and produces a new number from them. 
Also note that JavaScript also obeys the rule of BODMAS (Bracket Of Division Multiplication Addition Subtraction) which takes the order of
precedence respectively.
Meanwhile there is another operator called modulo, written as % which simply produces the remainder of two values after its division,example is 314 % 100 will produce 14 which is the remainder.
Special numbers:
These are special JavaScript values which are considered numbers but they don’t behave like normal numbers examples are the Infinity, -Infinity and NaN (Not a number). Infinity and -Infinity represent the positive and negative infinities while NaN is the value of two numbers that don’t yield a precise and meaningful result especially the when the values are both infinities.
STRINGS
This is another type of JavaScript value which is used to represent text, it is written by enclosing it’s content withing quotes which could be either single or double quotes. Examples of strings are;
 
“I’m in love with writing code and that makes me fortune”
  // I’m in love with writing code and that makes me fortune
‘fortune is my name, and I love to code’
  //fortune is my name, and I love to code

JavaScript producse a string type value when this is done, but there’s a bit of a problem when typing some characters. An example is;
‘I’m in love with writing code and that makes me fortune’
  //error
This will cause an error because JavaScript thinks the second single quote is the end of the string, thereby causing an error. However, we need to escape the second single quote using a backslash(\), so the appropriate way to write that code without getting an error should be this;
‘I\’m in love with writing code and that makes me fortune’ 
  // I’m in love with writing code and that makes me fortune
This means that backslashes makes any special characters part of the
string and doesn’t end it.

Arithmetic operations cannot be perform on strings but however, the addition operator can be used on them for concatenation - which glues different pieces of strings together an example of this is;
 
“for” + “tune”
  // fortune
There are more ways that we can manipulate strings, do some research on that to find out.
UNARY OPERATORS 
These are operators which take only one value in order to produce a result, most of the aforementioned operators usually operated on two values and are thus called binary operators. An example of unary operation;
 
console.log(typeof 4.5);
 // number

The typeof operator produces a string result naming the type of value given to it. The minus operator (-) can be used as both a unary operator and a
binary operator as seen below
 
console.log(- (10 -2));
// -8

BOOLEAN 
This is a type of JavaScript value which can be a true or a false, an on or an off, a yes or a no, which is used to distinguish between two possibilities. It is written as follows;
true
false
Comparisons
This is one way to produce a boolean result, which is based on the status of a condition. An example of this is;
console.log(3 > 2);
// true

Which simply means that 3 is greater than 2 for sure, which is why we see the true boolean result value.
Comparisons can also be performed on strings, at this point however, it is important to note that uppercase characters are always less than lowercase characters which means that “A” > “a” would return a false boolean result, non-alphabetic characters are also included in that ordering.
Some other similar operators are >=, <=, ==, and != which means greater than or equal to, less than or equal to, equal to and not equal to, respectively.
NB: NaN is the value which is not equal to itself.
Logical operators
There are three logical operators in JavaScript, they are NOT, AND, OR which can be used to reason about booleans.
The AND operator written as && produces a true boolean result if both values are true, the OR operator written as || produces a true when either of the values are true, while the NOT operator written as ! which is a unary operator that returns the opposite of the value given to it, which means !false will produce a true. it’s order of precedence are NOT AND OR respectively before the comparison operators.
TERNARY OPERATOR
This is a logical operator that operates on three values, it is written with a question mark and a colon, examples are;

console.log(true ? 3 : 4);
 // 3
console.log(false ? 3 : 4); 
 // 4

The example above picks the second value when the first value is a true and the third value when its a false.
UNDEFINED VALUES
These are values that denote the absence of meaningful data, they are written as null and undefined which can be used interchangeably
Automatic type conversion
JavaScript sometimes accept almost any program given to it, this includes programs that do odd things using type coercion which means that when an operator is applied to the wrong type of value JavaScript quietly converts that value to the type it wants using a set of rules, thereby producing an unwanted result. Examples are;
 
console . log (8 * null )
 // → 0
 console . log (“5” — 1)
 // → 4
 console . log (“5” + 1)
 // → 51
 console . log (“ five “ * 2)
 // → NaN
 console . log ( false == 0)
 // → true

The rules for converting strings and numbers to boolean values state that 0, NaN, and the empty string (“”) count as false, while other values count as true.
In situations where we do not want any automatic type conversions to occur, the usage of === and !== is recommended, as it does not allow any automatic type conversions and are precise in their results.
Short-circuiting of logical operators
This is an evaluation that handles logical operators (&& and ||) peculiarly by converting the value on their left side to boolean and producing a result depending on the outcome of the conversion of that value.
In this case the || operator returns the value on the left when its a true and will return the value on its right otherwise, while the && operator on the other hand returns the value on the left when its a false and will return the value on its right otherwise.

I believe that this gives a detailed summary of JavaScript value types and some operators, for further understanding of them it is always good to make research in order to learn more, in other words I will be posting JavaScript programming structure soon, so stay tuned and don’t stray. 
For questions, or corrections
please use the comment section and thank you very much for reading.

Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment