It shows [object object] in your alert message because you are trying to display an object instead of its properties. [Object object] is the default serialization of an object in JavaScript.
You can use JSOM or REST API to get current user as follows
JSOM
function getCurrentUser() {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
currentUser = web.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(onSuccess, onFail);
}
function onSuccess(sender, args) {
var acountname = currentUser.get_loginName(); // extract the login name from the account name
// var username = currentUser.get_title();
}
function onFail(sender, args) {
alert('Failed to get current user' + args.get_message());
}
using REST API // for SharePoint 2013
function getUser() {
var userid = _spPageContextInfo.userId;
// alert(userid);
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = { "accept": "application/json;odata=verbose" };
$.ajax({
url: requestUri,
contentType: "application/json;odata=verbose",
headers: requestHeaders,
success: onSuccess,
error: onError
}); function onSuccess(data, request) {
var loginName = data.d.Title;
alert(loginName);
} function onError(error) {
alert("Error on retrieving current user.");
}
}