DEV Community

Cover image for Values, Types, and Operators
Anasooya
Anasooya

Posted on • Updated on

Values, Types, and Operators

Did You know the computer can't understand our language except for machine language?

binary decimal
01000001 A
111 7
010 2

The binary code is well understood by our computers.


Editor:Vs-code,sublimetext,atom,fiddlejs,codesandbox,Repl,etc.

choose any one of the above mentioned editors.


VALUES:

Values comprised of bits, they play different roles. Values can be numbers, pieces of text or function, and so on.
NUMBERS:

Here we will discuss values of the number type i.e. numeric (12,13..) values.

OPERATORS:

UNARY OPERATOR

Typeof operator

console.log(typedef 34.15);
// *number
console.log(typedef "x");
// * string
Enter fullscreen mode Exit fullscreen mode

note: minus operator can be used as a binary as well as >unary operator.

console.log(-(5-2));
// -> -7
Enter fullscreen mode Exit fullscreen mode

BOOLEAN OPERATOR

Comparision
It tells us whether the value is true or false.

console.log(10>2);
//->true
console.log(5<9);
//->false
Enter fullscreen mode Exit fullscreen mode

We can also compare length of two strings.

console.log("ANA"<"SOOYA");
//->true
Enter fullscreen mode Exit fullscreen mode

Other operators are ==,>=,<=,!=

console.log("alpha"!="aphanhso");
//->true
Enter fullscreen mode Exit fullscreen mode

Note-There is only one value that is not equal to itself >and that is NaN (not a number)

console.log(NaN==NaN);
//->false
Enter fullscreen mode Exit fullscreen mode

LOGICAL OPERATOR

and is written as &&

console.log(true && false);
//->false
Enter fullscreen mode Exit fullscreen mode

or is written as ||

console.log(false||true);
//-->true
Enter fullscreen mode Exit fullscreen mode

Ternary operator ? or conditional operator

console.log(false?3:5);
//-->3
Enter fullscreen mode Exit fullscreen mode

Short-circuiting of logical operators

|| operator for example will return value to its left when >that can be converted to true and vice versa

console.log(null||"user");
//->user
Enter fullscreen mode Exit fullscreen mode

Automatic Type Conversion

At times javascript can accept any program you give it.Even if an operator applied to the "wrong" type of value,Javascript will convert it to that value it needs.This is known as type coercion.

console.log(4*null)
//->0
Enter fullscreen mode Exit fullscreen mode

In the above example, null becomes 0 so we got 0 as output.

console.log("2" - 1)
//->1
console.log("2"+1)
//->3
console.log("2"*1)
//->2
console.log("two"*1)
//->NaN
console.log(false==0)
//->true
Enter fullscreen mode Exit fullscreen mode

If null or undefined occurs on either side of the operator >,it produces true. Let's take an example.

console.log(null ==undefined);
//->true
Enter fullscreen mode Exit fullscreen mode

Ref: Eloquent Javascript

I would advice readers to code on their own and explore more.
If You have any doubts, you can write it in the comments section.

😃 Have an amazing day ahead.
Hope you liked this blog!

Top comments (0)