Javascript basics
Printing a string.
console.log("Hello World!");
Declare an variable and print it. The keyword var is needed to declare a variale.
var v='hey, this is a string'; console.log(v);
Get the length of a string.
var s='i am a string'; console.log(s.length); //13
Declare a number and print it.
var n=123456; console.log(n); //123456
if else statement.
var n=9; if(n%2==0) { console.log(n+" is an even number."); } else { console.log(n+" is an odd number."); }
Declaring arrays.
var strings=['cat','dog','bird']; //an array of strings var numbers=[2,3,5,6,8]; //an array of numbers var mix=[23,'hello',37,'kid']; //an array of strings and numbers
for loop.
var numbers=[23,45,67,23,45]; for(var i=0; i<numbers.length; i++) { console.log(numbers[i]); }
Declaring an object.
var pizza={ topping:['chicken','pineapple','ham'], crust: 'deep dish', size: 'large' }
Accessing properties of an object.
var pizza={ topping:['chicken','pineapple','ham'], crust: 'deep dish', size: 'large' } console.log(pizza.size);
Declaring a function.
function greeting(name) { console.log("Hello "+name); } console.log(greeting('Amy')); //Hello Amy
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts