JavaScript Data Types

JavaScript is loosely typed – it doesn’t care what the variables hold.

A variable could start off holding a number, then change to holding a character, a word, or anything else you want it to hold.

var temp="John Doe";
temp=123;
console.log(temp);//123

 

Data Types

Even though you don’t have to declare the data type up front, it’s still useful to know what types of data a variable can store, so that you can use and manipulate them properly inside your own programs.

JavaScript variables can be numbers, strings, and true or false values known as Booleans.
 

Numbers

Numbers in JavaScript come in two flavors: integers and floating point numbers.

var whole = 123;
var decimal = 1.23;

floats and integers can have negative values if you place a minus sign (-) in front of them:

var whole = -123;
var decimal = -1.23;

 

Strings

A String is a sequence of characters. Those characters could be letters, numbers, symbols, punctuation marks, or spaces..basically any character.

Strings are enclosed inside single(‘) or double(“) quotation marks.

var stringone = 'String 1 in single quotes';
var stringtwo = "String 2 in double quotes";

 
If you want to use a single or double quote inside the string, you can escape using backslash(\) character.

var text="She said \"Hi\"";
console.log(text); //She said "Hi"

If we want to print a double quote inside single quoted string or vice versa, we don’t need escape it.

var text='She said "Hi"';
console.log(text); //She said "Hi"

 

Special characters

We can use the backslash(\) with certain character literals as special characters.

Here is a list of special characters :

• \n: Newline
• \t: Tab
• \b: Backspace
• \r: Carriage return
• \\: Backslash
• \’: Single quote
• \”: Double quote
 

Boolean

A Boolean variable can only have a value of true or fa1se

true and false are keywords in JavaScript, so you don’t need to put any quote marks around them

var flagOn = true;
var flagOff = false;

 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags