JavaScript Data Types


String, Number, Boolean, Array(object), Object, Undefined, Null(object)

Note: In javascript Array return object data type and Null also

When adding a number and a string, JavaScript will treat the number as a string.

var x = 16 + 4 + "Volvo"; //  results  20Volvo

var x = "Volvo" + 16 + 4; // Volvo164

In the first example, JavaScript treats 16 and 4 as numbers, until it reaches "Volvo".

In the second example, since the first operand is a string, all operands are treated as strings.

JavaScript Has Dynamic Types

JavaScript has dynamic types. This means that the same variable can be used as different types:

var x;               // Now x is undefined
var x = 5;           // Now x is a Number
var x = "John";      // Now x is a String

String Example

var answer = "It's alright";             // Single quote inside double quotes
var answer = "He is called 'Johnny'";    // Single quotes inside double quotes
var answer = 'He is called "Johnny"';    // Double quotes inside single quotes

JavaScript Numbers

JavaScript has only one type of numbers.

Numbers can be written with, or without decimals:

var x1 = 34.00;     // Written with decimals
var x2 = 34;        // Written without decimals

Extra large or extra small numbers can be written with scientific (exponential) notation:

var y = 123e5;      // 12300000
var z = 123e-5;     // 0.00123

JavaScript Booleans

Booleans can only have two values: true or false.

var x = true;
var y = false;

JavaScript Arrays

JavaScript arrays are written with square brackets. Array items are separated by commas.

The following code declares (creates) an array called cars, containing three items (car names):

var cars = ["Saab", "Volvo", "BMW"];

Array indexes are zero-based, which means the first item is [0], second is [1], and so on.

JavaScript Objects

JavaScript objects are written with curly braces. Object properties are written as name:value pairs, separated by commas.

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

The typeof Operator

You can use the JavaScript typeof operator to find the type of a JavaScript variable:

typeof "John"                // Returns "string" 
typeof 3.14                  // Returns "number"
typeof false                 // Returns "boolean"
typeof [1,2,3,4]             // Returns "object" (not "array", see note below)
typeof {name:'John', age:34} // Returns "object"

The typeof operator returns "object" for arrays because in JavaScript arrays are objects.

Undefined

In JavaScript, a variable without a value, has the value undefined. The typeof is also undefined.

var person;                  // Value is undefined, type is undefined

Any variable can be emptied, by setting the value to undefined. The type will also be undefined.

person = undefined;          // Value is undefined, type is undefined

Empty Values

An empty value has nothing to do with undefined. An empty string variable has both a value and a type.

var car = "";                // The value is "", the typeof is "string"

Null

In JavaScript null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object. It should be null.

You can empty an object by setting it to null:

var person = null;           // Value is null, but type is still an object

You can also empty an object by setting it to undefined:

var person = undefined;     // Value is undefined, type is undefined

Difference Between Undefined and Null

typeof undefined             // undefined
typeof null                  // object
null === undefined           // false
null == undefined            // true

JavaScript String

Template Strings with Tagged Templates

const templateString = `This string starts and ends with a backtick and can have expressions, e.g. 5 + 5 = ${5+5}.
It can also have multiple lines without any odd "hacks".`
// Some dummy data... just I just call myself a dummy?
let user = {
    firstName: "Joseph",
    lastName: "Zimmerman",
    nickname: "Zim"
}
 
// Define the tag (definitely doesn't need to be called "tag")
function tag (...args) {
    // do something with those args
    // return something
}
 
// prefix with `tag` and foo will be set to whatever tag returns
let foo = tag`My name is ${user.firstName} ${user.lastName}, but my friends call me ${user.nickname}.`
var person = 'Mike';
var age = 28;

function myTag(strings, personExp, ageExp) {
  var str0 = strings[0]; // "That "
  var str1 = strings[1]; // " is a "

  // There is technically a string after
  // the final expression (in our example),
  // but it is empty (""), so disregard.
  // var str2 = strings[2];

  var ageStr;
  if (ageExp > 99){
    ageStr = 'centenarian';
  } else {
    ageStr = 'youngster';
  }

  // We can even return a string built using a template literal
  return `${str0}${personExp}${str1}${ageStr}`;
}

var output = myTag`That ${ person } is a ${ age }`;

console.log(output);
// That Mike is a youngster
const student = {
    name: "Ryan Christiani",
    blogUrl: "http://ryanchristiani.com"
}

const studentTemplate =  `<article>
    <h3>${'name'} is a student at HackerYou</h3>
    <p>You can find their work at ${'blogUrl'}.</p>
</article>`;

const myTemplate = studentTemplate(student);
alert(myTemplate);

Javascript String Methods

String.startsWith()

The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

str.startsWith(searchString[, position])
//startswith
var str = 'To be, or not to be, that is the question.';

console.log(str.startsWith('To be'));         // true
console.log(str.startsWith('not to be'));     // false
console.log(str.startsWith('not to be', 10)); // true
Note: The startsWith() method is case sensitive.

String.includes()

The includes() method determines whether one string may be found within another string, returning true or false as appropriate.

str.includes(searchString[, position])
var sentence = 'The quick brown fox jumps over the lazy dog.';

var word = 'fox';

console.log(`The word "${word}" ${sentence.includes(word)? 'is' : 'is not'} in the sentence`);
// expected output: "The word "fox" is in the sentence"
Note: The include() method is case sensitive.

Footer section 1

Content for the first footer section.

Footer section 2

Content for the second footer section.

Footer section 3

Content for the third footer section.