JAVASCRIPT BASICS. Type-Conversion in JavaScript. Type conversion or typecasting is one of the very important concept in

Similar documents
CSC Web Programming. Introduction to JavaScript

JAVASCRIPT BASICS. JavaScript String Functions. Here is the basic condition you have to follow. If you start a string with

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

HTML5 and CSS3 More JavaScript Page 1

CSE 431S Type Checking. Washington University Spring 2013

JavaScript CS 4640 Programming Languages for Web Applications

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

JavaScript: The Basics

c122mar413.notebook March 06, 2013

CSI33 Data Structures

Exercise 1: Basic HTML and JavaScript

JavaScript CS 4640 Programming Languages for Web Applications

Type Conversion. and. Statements

Topic 9: Type Checking

Topic 9: Type Checking

Such JavaScript Very Wow

introjs.notebook March 02, 2014

Programming language components

Types. What is a type?

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

COMS 469: Interactive Media II

Type Analysis. Type Checking vs. Type Inference

Midterm CSE 131B Spring 2005

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

3 The Building Blocks: Data Types, Literals, and Variables

Expressions and Casting

Subtyping (cont) Formalization of Subtyping. Lecture 15 CS 565. Inversion of the subtype relation:

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Chapter 2: Using Data

Bitwise Operator and Typecasting

JAVASCRIPT BASICS. JavaScript Math Functions. The Math functions helps you to perform mathematical tasks

Client-Side Web Technologies. JavaScript Part I

JavaScript: Introduction, Types

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

PES DEGREE COLLEGE BANGALORE SOUTH CAMPUS 1 K.M. before Electronic City, Bangalore WEB PROGRAMMING Solution Set II

Information Science 1

ORB Education Quality Teaching Resources

Subtyping (cont) Lecture 15 CS 565 4/3/08

Coding in JavaScript functions

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Introduction to Programming Using Java (98-388)

C-LANGUAGE CURRICULAM

Interfaces. Java interface. Notes. Example of code duplication. Why an interface construct? Interfaces & Types. good software engineering

3. Java - Language Constructs I

Expressions & Assignment Statements

Lab 1. Purpose. Assignment. Action Items/Programming Requirements

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Principles of Programming Languages

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Inheritance and object compatibility

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

Lab 1 Concert Ticket Calculator

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

B. V. Patel Institute of BMC & IT 2014

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

JAVASCRIPT LESSON 4: FUNCTIONS

Strict Inheritance. Object-Oriented Programming Spring 2008

C HAPTER S EVEN F OCUS ON J AVAS CRIPT AND VBSCRIPT E XTENSIONS

Chapter 5 Names, Binding, Type Checking and Scopes

Java Classes: Math, Integer A C S L E C T U R E 8

Lab 2 Population. Purpose. Assignment Lab 2 analyzes population growth of a town as well as compare the population growth of two towns.

CITS1231 Web Technologies. JavaScript Math, String, Array, Number, Debugging

CS-202 Introduction to Object Oriented Programming

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Inclusion Polymorphism

Introduction to JavaScript Programming Test Bank Chapter 1 with XML and PHP

Introduction & Java Review. EE 564 Lecture 1

LECTURE-2. Functions review HTML Forms. Arrays Exceptions Events. CS3101: Scripting Languages: Javascript Ramana Isukapalli

EECS1012. Net-centric Introduction to Computing. Lecture Introduction to Javascript

Binghamton University. CS-140 Fall Dynamic Types

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

Information Science 1

Programming Languages, Summary CSC419; Odelia Schwartz

Visual C# Instructor s Manual Table of Contents

A340 Laboratory Session #5

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Type Bindings. Static Type Binding

JewelScript Reference Manual

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Chapter 5 Object-Oriented Programming

Subtyping. Lecture 13 CS 565 3/27/06

Place User-Defined Functions in the HEAD Section

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Siebel escript Language Reference. Siebel Innovation Pack 2016 April 2016

PL / SQL Basics. Chapter 3

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 7. Expressions and Assignment Statements ISBN

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

1.3b Type Conversion

CS558 Programming Languages

Chapter 7. Expressions and Assignment Statements

Java Notes. 10th ICSE. Saravanan Ganesh

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

HAWK Language Reference Manual

Transcription:

Type-Conversion in JavaScript Description Type conversion or typecasting is one of the very important concept in JavaScript. It refers to changing an entity or variable from one datatype to another. There are two types of conversion: 1. Implicit Conversion - Coercion 2. Explicit Conversion - casting The term for implicit type conversion is coercion. Explicit type conversion in some specific way is known as casting. Implicit type conversion Implicit type conversion, also termed as coercion, is an automatic type conversion from one data type to another data type by the compiler itself. In mixed type expression, a subtype s will be converted to a supertype t or some subtypes s1, s2,... will be converted to a supertype t at runtime so that the program will run correctly. For example:

var x = 123; 100 + x ; The above statement recognized by the compiler for x as 123 which is a number and we will get the value as 100+123 = 223 (Addition operation is happening in this area) Sample code <! DOCTYPE html> <html> <body> <p>wikitechy - JavaScript Data Type Conversion </p> <p id="wikitechy_text"></p> <script> var x = 100; document.getelementbyid("wikitechy_text").innerhtml = x+100; </script> </body> </html> Let s try with an another example, now the value of x is a var x = 'a'; x+100; The above line will recognize x as a character or string and the output is a100 (String Concatenation happening in this)

Sample code <! DOCTYPE html> <html> <body> <p>wikitechy - Javascript Data Type Conversion </p> <p id="wikitechy_text"></p> <script> var x = abc; document.getelementbyid("wikitechy_text").innerhtml =x+100; </script> </body> </html> Explicit type conversion In this case, an explicit type conversion will happen. The variables will be explicitly converted from one type to another type. For Example: parseint("3.14") - > This function will parse the value given in the brackets and convert in to integer. So, the output of the above command is 3. String(Date()) -> Get current date and time and convert it into String.

Sample code <!DOCTYPE html> <html> <body> <p>wikitechy - Javascript Data Type Conversion </p> <p id="wikitechy_text"></p> <script> var x = 123; document.getelementbyid("wikitechy_text").innerhtml = String(x) + "<br>" + String(123) + "<br>" + String(100 + 23)+ "<br>" + "<br>"+ "<br>"+ "<br>"+ x.tostring()+ "<br>" + (123).toString()+ "<br>" + (100 + 23).toString()+ "<br>" + "<br>"+ String(Date())+ "<br>" + Date().toString()+ "<br>" + "<br>"+ "<br>"+ </script> </body> </html> Number("3.14") + "<br>" + parsefloat("3.14")+ "<br>" + parseint("3.14");

Code Explanation: JAVASCRIPT BASICS Paragraph tag displaying the text WikiTechy - Javascript Arraus Paragraph tag in which we are trying to get the result. The id of the paragraph tag is, WikiTechy_Text <p id="wikitechy_text"></p> Script tag -> Entire JavaScript functions and variables needs to be declared between the tags. <script>.</script> Initialize the variable x var; x = 123;

Trying to fetch the element in the HTML WikiTechy_Text and accessing the innerhtml value and assign the properties of the object. Type casting of the variable x into String. Variable assigning method String(x) + "<br>" + The output will be 123 String(123) + "<br>" + The output will be 123 String(100 + 23)+ "<br>" + "<br>"+ "<br>"+ The output will be 123 Type casting of the variable x into String using tostring() method. x.tostring()+ "<br>" + The output will be 123 (123).toString()+ "<br>" + The output will be 123 (100 +23).toString() + "<br>" + "<br>"+ "<br>"+ The output will be 123 Type casting of the variable x into String using tostring() method. String(Date())+ "<br>" + -> The output is Thu Apr 21 2016 06:57:02 GMT+0530 (India Standard Time) Date().toString()+ "<br>" + "<br>"+ "<br>" -> The output is Thu Apr 21 2016 06:57:02 GMT+0530 (India Standard Time) Type Conversion for numbers. Trying to get the integers and float values in the below code Number("3.14") + "<br>" + -> The output is a number 3.14 parsefloat("3.14")+ "<br>" + -> The output is a number 3.14 parseint("3.14"); -> The output is a number 3

Sample Output: JAVASCRIPT BASICS

Output:

Displaying the text WikiTechy - JavaScript Arrays in the paragraph tag of the HTML page. Displaying the result String(x) + "<br>" + The output will be 123 String(123) + "<br>" + The output will be 123 String(100 + 23)+ "<br>" + "<br>"+ "<br>"+ The output will be 123 Displaying the next result x.tostring()+ "<br>" + The output will be 123 (123).toString()+ "<br>" + The output will be 123 (100+23).toString() + "<br>" + "<br>"+ "<br>"+ The output will be 123 Displaying the date values String(Date())+ "<br>" + -> The output is Thu Apr 21 2016 06:57:02 GMT+0530 (India Standard Time) Date().toString()+ "<br>" + "<br>"+ "<br>" -> The output is Thu Apr 21 2016 06:57:02 GMT+0530 (India Standard Time) Displaying the number values Number("3.14") + "<br>" + -> The output is a number 3.14 parsefloat("3.14")+ "<br>" + -> The output is a number 3.14 parseint("3.14"); -> The output is a number 3