Type Conversions in JavaScript

0
0

Most of the time, the functions and operators automatically convert the values given then to the right type. An alert automatically converts any value to a string to show it. Mathematical operations change the importance of numbers.   

JavaScript is a client-side, object-based scripting language that is used to control and verify client-side data. JavaScript, earlier known as LiveScript, has been developed by Netscape and Sun Microsystems. Are you looking for Javascript Training in Chennai? Take a step into FITA, Enhance your knowledge with us for employability. FITA is the best leading institution for Javascript Course in Chennai

String Conversion 

String conversion happens, when we need the string form of a value. For example, the alert(value) does it to show the value. We can also call the String(value) function to convert a value to a string. 

let value= true;

alert(typeof value);//boolean

value=String(value); // now value is a string "true"  

alert(typeof value); // string

String conversion is mostly obvious. A false becomes "false", null becomes "null", etc...

Numeric Conversion

Numeric conversion happens for mathematical purposes and expressions automatically. 

For example, when division/ is applied to non-numbers:

alert( "6" / "2" ); // 3, string are converted to numbers. we can also use the Number(value) function to explicity convert a value to a number.

let str = "123";

alert(typeof str); // string

let num = Number(str); // becomes a number 123

alert(typeof num); // number

Explicit conversion is needed when we read a value from a string-based source like a text form but require a number to be entered. If the string is not a valid number, the result of such a conversion is NaN. 

let age= Number("an arbitrary string instead of a number");

alert(age); // NaN, change failed

Examples:

alert( Number(" 123 ") ); // 123

alert( Number("123z") ); // NaN (error reading a number at "z")

alert( Number(true) ); // 1

alert( Number(false) ); // 0

Please note that null and irregular function differently here: null becomes zero while undefined becomes NaN. Most mathematical engineers also make such a change.

Boolean conversion

Boolean conversion is the simplest one.

It may happen in logical operations (later we’ll meet condition tests and other similar things), but can also be performed explicitly with a call to boolean(value).  

The conversion rule

Values that are intuitively "empty", like, an empty string, null, undefined, and NaN become false. Other values become true. 

For instance:

alert( Boolean(1) ); // true

alert( Boolean(0) ); // false

alert( Boolean("hello") ); // true

alert( Boolean("") ); // false

JavaScript is commonly used as a part of web pages. It is an interpreted programming language with Object-Oriented capabilities. Are you looking for Javascript Training in Bangalore? Take a step into FITA, Enhance your knowledge with us for employability. FITA is the best leading institution for Javascript Course in Bangalore. | Javascript Coaching in Bangalore | 

 

Zoeken
Categorieën
Read More
Other
Rola Rodziny W Wychowaniu Chrześcijańskim Na Odległości XIX I XX Stulecia
Art. 113. 1. (wygasł) 2. Dotychczasowe prawa do wykonywania nauk a różnych...
By Wiersze Opowiadania 2021-09-18 21:34:41 0 0
Other
Sativum Complex-ส่วนผสมจากธรรมชาติ, ผลข้างเคียง & การร้องเรียน?
คุณเป็นผู้ป่วยโรคเบาหวานหรือไม่?คุณกำลังมองหาสูตรต้านเบาหวานที่ช่วยให้คุณมีชีวิตที่แข็งแรงอยู่หรื...
By Stephni Edabel 2021-09-23 07:16:38 0 0
Other
MENGUASAI Teknik MANIAK DALAM BERMAIN POKER ONLINE
Mengabuk Aturan Maniak Dalam Bermain Poker Online ? Bakal sanggup mengalahkan satu cagaran...
By Hoover Floyd 2021-09-19 20:02:15 0 0
Other
Automotive Shock Absorber Market 2021: Industry Size, Top Companies, Segments and Forecast to 2027
Overview The Automotive Shock Absorber Market is anticipated to grow by US$21,830.1 million...
By Rohan Kkk 2021-04-20 05:01:52 0 0
Other
8 Principais Causas De Disfunção Erétil Impotência Masculina
É possível escolher entre próteses maleáveis (semirrígidas),...
By Waters Groth 2021-09-14 22:57:10 0 0